Skip to content

Commit a0c5b85

Browse files
authored
Update README.md
Fixes #3 Fixes #7 Fixes #8 Fixes #9 Fixes #10
1 parent 555e576 commit a0c5b85

File tree

1 file changed

+74
-25
lines changed

1 file changed

+74
-25
lines changed

README.md

+74-25
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ nf.formatRangeToParts(3, 5);
6262
*/
6363
```
6464

65-
When both sides of the range resolve to the same value after rounding, the display will fall back to the approximately sign (see below). The automatic approximately sign will occur only if `signDisplay` is set to `"auto"`; otherwise, the user's requested `signDisplay` will be used.
65+
When both sides of the range resolve to the same value after rounding, an approximately sign will be added.
6666

6767
```javascript
6868
const nf = new Intl.NumberFormat("en-US", {
@@ -71,37 +71,83 @@ const nf = new Intl.NumberFormat("en-US", {
7171
maximumFractionDigits: 0,
7272
});
7373
nf.formatRange(2.9, 3.1); // "~€3"
74+
75+
const nf = new Intl.NumberFormat("en-US", {
76+
style: "currency",
77+
currency: "EUR",
78+
signDisplay: "always",
79+
});
80+
nf.formatRange(2.900, 3.001); // "~+€3.00"
7481
```
7582

7683
## Grouping Enum ([ECMA-402 #367](https://github.com/tc39/ecma402/issues/367))
7784

78-
Currently, Intl.NumberFormat accepts a `{ useGrouping }` option, which accepts a boolean value. However, as reported in the bug thread, there are several options users may want when speficying grouping. This proposal is to add the following strings as valid inputs to `{ useGrouping }`:
85+
Main Issue: [#3](https://github.com/tc39/proposal-intl-numberformat-v3/issues/3)
86+
87+
Currently, Intl.NumberFormat accepts a `{ useGrouping }` option, which accepts a boolean value. However, as reported in the bug thread, there are several options users may want when speficying grouping. This proposal is to make the following be valid inputs to `{ useGrouping }`:
7988

89+
- `false`: do not display grouping separators
8090
- `"min2"`: display grouping separators when there are at least 2 digits in a group; for example, "1000" (first group too small) and "10,000" (now there are at least 2 digits in that group).
8191
- `"auto"` (default): display grouping separators based on the locale preference, which may also be dependent on the currency. Most locales prefer to use grouping separators.
82-
- `"always"` (`true`): display grouping separators even if the locale prefers otherwise.
92+
- `"always"`: display grouping separators even if the locale prefers otherwise.
93+
- `true`: alias for `"always"`
94+
- `undefined` (default): alias for `"auto"`
8395

84-
Previously considered was an option `"never"` corresponding to the current value `false`. The current proposal does not add that option, because `false` will continue to work, and since non-empty strings are truthy, `if(nf.resolvedOptions().useGrouping)` will continue to work as expected.
96+
In `resolvedOptions`, either `false` or one of the three strings will be returned. This is an observable behavior change, because currently only the booleans `true` and `false` are returned.
8597

8698
## New Rounding/Precision Options ([ECMA-402 #286](https://github.com/tc39/ecma402/issues/286))
8799

88-
Additional Context: [Unified NumberFormat #9](https://github.com/tc39/proposal-unified-intl-numberformat/issues/9)
100+
Main Issue: [#8](https://github.com/tc39/proposal-intl-numberformat-v3/issues/8
89101

90-
Currently, Intl.NumberFormat allows for two rounding strategies: min/max fraction digits, or min/max significant digits. Those strategies cannot be combined.
102+
Additional Context: [Unified NumberFormat #9](https://github.com/tc39/proposal-unified-intl-numberformat/issues/9)
91103

92-
I propose adding the following options to control rounding behavior:
104+
The following additional options are proposed to the Intl.NumberFormat options bag to control rounding behavior:
93105

106+
- `roundingPriority` = a string set to either `"significantDigits"`, `"morePrecision"`, or `"lessPrecision"` (details below)
94107
- `roundingIncrement` = an integer, either 1 or 5 with any number of zeros.
95108
- Example values: 1 (default), 5, 10, 50, 100
96-
- Nickel rounding: `{ maximumFractionDigits: 2, roundingIncrement: 5 }`
97-
- Dime rounding: `{ maximumFractionDigits: 2, roundingIncrement: 10 }`
98-
- `trailingZeros` = an enum expressing the strategy for resolving trailing zeros when combining min/max fraction and significant digits.
99-
- `"auto"` = obey mininumFractionDigits or minimumSignificantDigits (default behavior).
100-
- `"strip"` = always remove trailing zeros.
101-
- `"stripIfInteger"` = remove them only when the entire fraction is zero.
102-
- *optional:* `"keep"` = always keep trailing zeros according to the rounding magnitude.
109+
- Nickel rounding: `{ minimumFractionDigits: 2, maximumFractionDigits: 2, roundingIncrement: 5 }`
110+
- Dime rounding: `{ minimumFractionDigits: 2, maximumFractionDigits: 2, roundingIncrement: 10 }`
111+
- `trailingZeroDisplay` = a string expressing the strategy for displaying trailing zeros on whole numbers:
112+
- `"auto"` = current behavior. Keep trailing zeros according to minimumFractionDigits and minimumSignificantDigits.
113+
- `"stripIfInteger"` = same as `"auto"`, but remove the fraction digits if they are all zero.
114+
115+
### Rounding Priority
116+
117+
Currently, Intl.NumberFormat allows for two rounding strategies: min/max fraction digits, or min/max significant digits. Currently, if both min/max fraction digits and min/max significant digits are both specified, significant digit settings take priority and fraction digit settings are ignored.
118+
119+
The new option `roundingPriority` specifies two new strategies to resolve mixed fraction digits and significant digits settings. To best express the new strategies, consider the following option bag:
120+
121+
```
122+
{
123+
maximumFractionDigits: 2,
124+
maximumSignificantDigits: 2
125+
}
126+
```
127+
128+
The above options should be interpreted to mean:
129+
130+
1. Round the number at the hundredths place
131+
2. Round the number after the second significant digit
132+
133+
Now, consider the number "4.321". `maximumFractionDigits` wants to round at the hundredths place, producing "4.32". However, `maximumSignificantDigits` wants to round after two significant digits, producing "4.3". We therefore have a conflict.
103134

104-
The exact semantics of how to allow fraction digits and significant digits to interoperate are being tracked by [#8](https://github.com/tc39/proposal-intl-numberformat-v3/issues/8).
135+
The new setting `roundingPriority` offers a hint on how to resolve this conflict. There are three options:
136+
137+
1. `roundingPriority: "significantDigits"` means that significant digits always win a conflict.
138+
2. `roundingPriority: "morePrecision"` means that the result with more precision wins a conflict.
139+
3. `roundingPriority: "lessPrecision"` means that the result with less precision wins a conflict.
140+
141+
This resolution algorithm applies separately between the maximum digits settings and the minimum digits settings. So, for example, suppose you had
142+
143+
```
144+
{
145+
minimumFractionDigits: 2,
146+
minimumSignificantDigits: 2
147+
}
148+
```
149+
150+
Consider the input number "1". `minimumFractionDigits` wants to retain trailing zeros up to the hundredths place, producing "1.00", whereas `minimumSignificantDigits` wants to retain only as many as are required to render two significant digits, producing "1.0". We again have a conflict, and the conflict is resolved in the same way.
105151

106152
## Interpret Strings as Decimals ([ECMA-402 #334](https://github.com/tc39/ecma402/issues/334))
107153

@@ -119,18 +165,21 @@ We will reference existing standards for interpreting decimal number strings whe
119165

120166
## Rounding Modes ([ECMA-402 #419](https://github.com/tc39/ecma402/issues/419))
121167

122-
Intl.NumberFormat always performs "half-up" rounding (for example, if you have 2.5, it gets rounded to 3). However, we understand that there are users and use cases that would benefit from exposing more options for rounding modes.
123-
124-
The list of rounding modes could be:
168+
Main Issue: [#7](https://github.com/tc39/proposal-intl-numberformat-v3/issues/7)
125169

126-
1. `"halfUp"` (default)
127-
2. `"halfEven"`
128-
3. `"halfDown"`
129-
4. `"ceiling"`
130-
5. `"floor"`
131-
6. `"up"`
132-
7. `"down"`
170+
Intl.NumberFormat always performs "half-up" rounding (for example, if you have 2.5, it gets rounded to 3). However, we understand that there are users and use cases that would benefit from exposing more options for rounding modes.
133171

172+
The list of rounding modes is proposed to be:
173+
174+
1. ceil (toward +∞)
175+
2. floor (toward -∞)
176+
3. expand (away from 0)
177+
4. trunc (toward 0)
178+
5. halfCeil (ties toward +∞)
179+
6. halfFloor (ties toward -∞)
180+
7. halfExpand (ties away from 0; current behavior; default)
181+
8. halfTrunc (ties toward 0)
182+
9. halfEven (ties toward the value with even cardinality)
134183

135184
## v8 Prototype
136185
A prototype of the latest spec text can be found in https://chromium-review.googlesource.com/c/v8/v8/+/2336146 w/ the flag --harmony_intl_number_format_v3

0 commit comments

Comments
 (0)