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

Move scale to the format method? #1

Closed
sffc opened this issue Mar 26, 2020 · 2 comments
Closed

Move scale to the format method? #1

sffc opened this issue Mar 26, 2020 · 2 comments

Comments

@sffc
Copy link
Collaborator

sffc commented Mar 26, 2020

The main use case that's interesting for scale is when you have a BigInt of a small unit, like currency micros, and you want to divide it by some constant when formatting.

This raises the question: perhaps, since the scale is assumed to be more related to the number than the formatter settings, that scale should go into the format method. For example:

const nf = new Intl.NumberFormat("fr-FR", {
    style: "currency",
    currency: "EUR",
});
const micros = 1000000n;

// Version 1:
nf.format(micros, { scale: -6 });

// Version 2:
nf.format({
    number: micros,
    scale: -6
});
@sffc
Copy link
Collaborator Author

sffc commented May 2, 2020

We discussed this in the TG2 meeting.

We identified a fatal flaw in the currently proposed solution of putting scale in the main options bag. If you write the following code:

new Intl.NumberFormat("en", { scale: -6 }).format(1e6)

the correct result is "1". However, if you run the same code in older browsers, or browsers that don't support this feature yet, you get "1,000,000", without any errors. In other words, you could have a situation where a new car costs $500 in one browser, but $50,000 in another.

This problem is unique to scale, because most of the other features we added in Unified NumberFormat are display styles, where the number being rendered does not change. (Measurement units were in the style property, which would cause an exception.)

The option that would mitigate this problem would be "Version 2" from above. However, the committee didn't like the ergonomics of that option.

The conclusion is to therefore lean on the Decimal proposal to make an exact way to change the scale of a number, and in the mean time, allow for full-precision strings to be formatted.

@stiff
Copy link

stiff commented Jan 20, 2021

Maybe d3.js-style approach would fit nice here:
new Intl.NumberFormat("en").scale(-6).format(1e6)

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

No branches or pull requests

2 participants