Skip to content

Intl.NumberFormat support #15

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

Open
littledan opened this issue Nov 13, 2019 · 5 comments
Open

Intl.NumberFormat support #15

littledan opened this issue Nov 13, 2019 · 5 comments

Comments

@littledan
Copy link
Member

It seems like Intl.NumberFormat.prototype.format should support BigDecimal transparently, just as it supports BigInt.

On the ICU level, this should be straightforward, using the same API as BigInt uses.

On a function API level, we have clear precedent with BigInt that we should overload the format and formatToParts methods.

The complexity comes in for options processing: NumberFormat is based on rounding the input, but BigDecimal is all about avoiding implicit rounding. How should that all be handled?

@littledan
Copy link
Member Author

cc @sffc

@littledan
Copy link
Member Author

The complexity comes in for options processing: NumberFormat is based on rounding the input, but BigDecimal is all about avoiding implicit rounding. How should that all be handled?

Maybe this mismatch goes away if BigDecimals are always normalized? The main difference would be in how the default is treated: BigDecimals should omit rounding by default, I think, not round on Number's terms. If rounding options are used explicitly, then great, no mismatch.

@sffc
Copy link

sffc commented Nov 21, 2019

I think it would be reasonable if BigDecimal had a different default rounding strategy than Number (or BigInteger), but it should still allow Intl.NumberFormat to apply its own rounding. For example, when rendering a currency, you should obey the currency rounding rules.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Mar 12, 2025

Given some discussions from the past few months, I'm thinking that maybe we should make Intl.NumberFormat.prototype.format throw when it receives a decimal, at least if the constructor didn't explicitly receive some options about the precision of the number.

I have been convinced that, for localization/display purposes, the precision is important and should be explicit somehow, either as new Intl.NumberFormat({ minSignificantDigits: 2, minSignificantDigits: 4 }).format(decimal) or as new Intl.NumberFormat().format(decimal.toAmount({ significantDigits: 2 })).

@sffc
Copy link

sffc commented Mar 12, 2025

I agree with @nicolo-ribaudo, in the sense that a "point on a number line" is a machine concept and not intrinsically a localizable value. It is localizable after the scale of the value is specified.

I could still be convinced that we should treat normalized decimals the same as (normalized) floats to make things like migrating code bases easier. But this seems like something easy to add later when we have more evidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants