-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Accept decimal strings in Intl.NumberFormat #57595
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
Conversation
164b246
to
7d5a85a
Compare
👍 (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 |
Rebased. The new structure is nice! |
7d5a85a
to
97ff57e
Compare
This makes sense to me. I'll bring the issue up at a design meeting. |
933fc24
to
a8c6947
Compare
We agreed that this makes sense. @felixfbecker would you mind bringing the PR up to date with main? |
a8c6947
to
bd132ea
Compare
@sandersn Awesome, I rebased onto |
…g. Format method takes a string in the spec and will be updated by TypeScipt team soon. See microsoft/TypeScript#57595
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 housesformatRange()
, which also got added in v3): https://github.com/tc39/proposal-intl-numberformat-v3#interpret-strings-as-decimals-ecma-402-334The format matches what
${number}
accepts (plusInfinity
):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 castingas any
. The resulting type errors currently misleads developers into usingparseFloat()
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