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

NumberFormat: Hide fraction digits when formatting an integer #286

Open
evilpie opened this issue Oct 21, 2018 · 9 comments
Open

NumberFormat: Hide fraction digits when formatting an integer #286

evilpie opened this issue Oct 21, 2018 · 9 comments
Assignees
Labels
c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: in progress Status: the issue has an active proposal

Comments

@evilpie
Copy link

evilpie commented Oct 21, 2018

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.

@theophani
Copy link

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.

@sffc
Copy link
Contributor

sffc commented Oct 23, 2018

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.

@theophani
Copy link

theophani commented Oct 23, 2018 via email

@sffc
Copy link
Contributor

sffc commented Oct 23, 2018

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
}

@sffc sffc added c: numbers Component: numbers, currency, units s: comment Status: more info is needed to move forward labels Mar 19, 2019
@yecril71pl
Copy link

Please change the description of this issue to something more user-friendly! Thank you!

@theophani
Copy link

@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?

@yecril71pl
Copy link

This issue is specifically about currency, and whether minor units are displayed and when. The issue summary does not tell me that.

@sffc sffc added s: discuss Status: TG2 must discuss to move forward and removed s: comment Status: more info is needed to move forward labels Jan 8, 2020
@sffc
Copy link
Contributor

sffc commented Jan 8, 2020

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.

@sffc sffc added this to the ES 2021 milestone Mar 20, 2020
@sffc sffc changed the title NumberFormat doesn't allow "human friendly" format NumberFormat: Hide fraction digits when formatting an integer Mar 20, 2020
@sffc sffc added s: help wanted Status: help wanted; needs proposal champion and removed s: discuss Status: TG2 must discuss to move forward labels Mar 20, 2020
@sffc
Copy link
Contributor

sffc commented Mar 21, 2020

I plan to address this as part of my new proposal Intl.NumberFormat V3.

https://github.com/sffc/proposal-intl-numberformat-v3

@sffc sffc added s: in progress Status: the issue has an active proposal and removed s: help wanted Status: help wanted; needs proposal champion labels Mar 21, 2020
@sffc sffc self-assigned this Jun 5, 2020
@sffc sffc added the Proposal Larger change requiring a proposal label Jun 5, 2020
@sffc sffc removed this from the ES 2021 milestone Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: in progress Status: the issue has an active proposal
Projects
No open projects
Development

No branches or pull requests

4 participants