-
Notifications
You must be signed in to change notification settings - Fork 107
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
NumberFormat: Hide fraction digits when formatting an integer #286
Comments
Thanks @evilpie. Note also that new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0
}).format(12.333); rather reasonably returns new Intl.NumberFormat('en-US', {
minimumFractionDigits: 0
}).format(12.333); returns In other words, This same awareness should be applied in extending the precision when there are any significant minor currency units, if ps: I would suggest that display values such as “$12.3” are also not valid according to either ISO 4217 or Unicode CLDR. |
To get integer rounding behavior when currency style is used, you need to set both minimum and maximum fraction digits: (12.333).toLocaleString("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 0,
maximumFractionDigits: 0
}); Here is the spec for how these options are resolved: https://tc39.github.io/ecma402/#sec-setnfdigitoptions After I'm done with the current Intl.NumberFormat feature proposal, I'd like to work on a better API for rounding strategies. |
Thanks for this clarification. It will be helpful to those seeking to accomplish integer rounding. (Though, one could round the number oneself first, before applying formatting.)
However, I don’t want integer rounding. What I’m looking for is for the minor units (decimal places) to be omitted when the number is already an integer (or has no significant minor currency units) and for at least n digits of precision when there any minor units, where n is the currency exponent.
Representations such as “$12.5” are not valid, and should not be returned as a currency representation.
|
This makes sense. There should be a way to set this preference within Intl.NumberFormat. In the mean time, your best option for now is to do this logic externally to Intl.NumberFormat; something like this: if (Number.isInteger(num)) {
// set min/max fraction digits
} else {
// use default min/max fraction digits
} |
Please change the description of this issue to something more user-friendly! Thank you! |
@yecril71pl I can’t tell if this is a real request, or a joke :). Sorry to ruin your joke if it is one, but can you confirm? |
This issue is specifically about currency, and whether minor units are displayed and when. The issue summary does not tell me that. |
We keep getting requests for this feature, including internally at Google. I think it's small enough that we can go through the Normative PR process. I suggest waiting for Unified NumberFormat to be merged, since this may produce a merge conflict that amounts to re-writing the patch. |
I plan to address this as part of my new proposal Intl.NumberFormat V3. https://github.com/sffc/proposal-intl-numberformat-v3 |
This was triggered by tweets from @theophani: https://twitter.com/theophani/status/1054118666852278273.
NumberFormat doesn't actually allow you to format numbers in a way that I would say is "human friendly". Let's take her example. If we have a whole number like 12, we want to display that as a simple $12, but if we've got something like 12.3 we want to display that as $12.30. As far as I can tell there is no way to do this with the current API.
minimumFractionDigits: 0
gets kind of close, but it's not good enough: it gives you $12 for 12.0, but $12.3 for 12.3 instead of $12.30.I skimmed through the issue list, but nothing obvious popped out at me, so sorry if this is a duplicate.
The text was updated successfully, but these errors were encountered: