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

Add more rounding modes #419

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

Add more rounding modes #419

sffc opened this issue Mar 26, 2020 · 6 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

@sffc
Copy link
Contributor

sffc commented Mar 26, 2020

@littledan suggested adding rounding modes to the Intl.NumberFormat V3 proposal.

The spec currently says:

Let n be an integer for which the exact mathematical value of n ÷ 10^(f – x( is as close to zero as possible. If there are two such n, pick the larger n.

In other words, numbers are always rounded "half up". However, this is not always what users want.

ICU supports a longer list of rounding modes:

http://userguide.icu-project.org/formatparse/numbers/rounding-modes

  • Ceiling
  • Floor
  • Down
  • Up
  • Half-even
  • Half-up
  • Half-down
@sffc sffc added s: in progress Status: the issue has an active proposal c: numbers Component: numbers, currency, units labels Mar 26, 2020
@sffc sffc self-assigned this Jun 5, 2020
@sffc sffc added the Proposal Larger change requiring a proposal label Jun 5, 2020
@sffc
Copy link
Contributor Author

sffc commented Jun 16, 2020

According to this table, "ceiling" rounds numbers toward positive infinity for all numbers; "up" rounds positive numbers toward positive infinity and negative numbers toward negative infinity. So, I think "half-up" is what 402 is doing.

For example:

(-1.5).toLocaleString("en", {maximumFractionDigits: 0})
// "-2"

That would be "-1" for "half-ceiling".

@Yaffle
Copy link

Yaffle commented Jun 16, 2020

@sffc , right, sorry, my mistake

@Yaffle
Copy link

Yaffle commented Jan 6, 2021

Should Number#toPrecision, Number#toFixed and Number#toExponential also be updated? This will allow -BigDecimal- BigFloat ( with interface described in https://github.com/tc39/proposal-decimal ) to have the same abilitiy. Btw, for me, it is interesting to support only "half-down" and "half-up", seems.

@sffc
Copy link
Contributor Author

sffc commented Jan 7, 2021

Should Number#toPrecision, Number#toFixed and Number#toExponential also be updated? This will allow -BigDecimal- BigFloat ( with interface descrpibed in https://github.com/tc39/proposal-decimal ) to have the same abilitiy. For me, it is interesting to support only "half-down" and "half-up", seems.

This proposal will set that type of precedent, but it is beyond the scope of what I plan to do.

By the way, the main issue for discussion of rounding modes can be found in the proposal repo: tc39/proposal-intl-numberformat-v3#7

@Yaffle
Copy link

Yaffle commented Jan 7, 2021

@sffc, Thanks for the answer, Well, my vote is for "Interpret Strings as Decimals" and the "bigfloat" library will do the rounding in my use case, and to keep the trailing zeros as is (no add, no remove).

@sffc
Copy link
Contributor Author

sffc commented Oct 24, 2024

This is landed as part of Intl.NumberFormat v3.

@sffc sffc closed this as completed Oct 24, 2024
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
None yet
Development

No branches or pull requests

2 participants