Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept decimal strings in Intl.NumberFormat #57595

Merged
merged 3 commits into from
Apr 5, 2024

Conversation

felixfbecker
Copy link
Contributor

@felixfbecker felixfbecker commented Mar 1, 2024

Fixes #52124

Note: This is not a coercion like in other JS APIs that also "accept" strings by coercing them, it is specifically designed to handle arbitrary-length decimals without precision loss. This was added as an explicit feature in Intl.NumberFormat v3 (which is why I added it to es2023.intl, which also houses formatRange(), which also got added in v3): https://github.com/tc39/proposal-intl-numberformat-v3#interpret-strings-as-decimals-ecma-402-334

The format matches what ${number} accepts (plus Infinity):

The general syntax for decimal strings, essentially #.#E#, is a widely understood format in computing. The specific version we use is the ECMA-262 StringNumericLiteral grammar, which also allows non-base-10 numbers like hexadecimal and binary.

Spec link: https://tc39.es/ecma402/#sec-runtime-semantics-stringintlmv

It is very common to represent monetary numbers as strings in JSON APIs to avoid any precision loss and those can currently not be passed to the Intl.NumberFormat API without casting as any. The resulting type errors currently misleads developers into using parseFloat() to make the type error go away, which causes precision loss.

It is supported by all browsers: https://caniuse.com/mdn-javascript_builtins_intl_numberformat_format_number_parameter-string_decimal

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Mar 1, 2024
@Renegade334
Copy link
Contributor

👍 (this is the relevant feature in the spec – strings are now converted to mathematical values, rather than coerced to numbers as in previous versions)

FYI, the ECMA-402 v10 NumberFormat definitions were moved to es2023.intl since your branch, so this will need updating.

@felixfbecker
Copy link
Contributor Author

Rebased. The new structure is nice!

@sandersn
Copy link
Member

This makes sense to me. I'll bring the issue up at a design meeting.

@sandersn
Copy link
Member

sandersn commented Apr 5, 2024

We agreed that this makes sense. @felixfbecker would you mind bringing the PR up to date with main?

@felixfbecker
Copy link
Contributor Author

@sandersn Awesome, I rebased onto main! Build is passing.

@sandersn sandersn merged commit bdd1f94 into microsoft:main Apr 5, 2024
25 checks passed
@felixfbecker felixfbecker deleted the intl-numberformat-v3 branch April 5, 2024 21:12
simonrevill pushed a commit to simonrevill/tdd-shopping-cart that referenced this pull request Apr 13, 2024
…g. Format method takes a string in the spec and will be updated by TypeScipt team soon. See microsoft/TypeScript#57595
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Intl.NumberFormat.prototype.format parameter type is too strict
4 participants