tc39 / ecma402

Status, process, and documents for ECMA 402

Home Page:https://tc39.es/ecma402/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NumberFormat: Hide fraction digits when formatting an integer

evilpie opened this issue · comments

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.

Thanks @evilpie.

Note also that

new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 0
}).format(12.333);

rather reasonably returns $12.33 and not $12.333, even though

new Intl.NumberFormat('en-US', {
  minimumFractionDigits: 0
}).format(12.333);

returns 12.333.

In other words, Intl.NumberFormat rounds currencies to the appropriate digits of precision to cover the minor currency units. This behaviour is as I would expect and hope.

This same awareness should be applied in extending the precision when there are any significant minor currency units, if minimumFractionDigits is specified and is less than the currency “exponent”.

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.

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