tc39/ecma402

Add more rounding modes

sffc opened this issue · 6 comments

sffc commented

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

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".

@sffc , right, sorry, my mistake

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 commented

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

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

This is landed as part of Intl.NumberFormat v3.