Skip to content

Commit

Permalink
Intl.NumberFormat. v3 fixes (#28023)
Browse files Browse the repository at this point in the history
* NumberFormat.resolvedOptions fixes

* Update files/en-us/web/javascript/reference/global_objects/intl/numberformat/resolvedoptions/index.md

* Add unit, unitdisplay to the resolvedOptions. Add some exceptions to the constructor

* Add TypeError if unit or currency not defined for that style of formatting
  • Loading branch information
hamishwillee committed Jul 20, 2023
1 parent c14d0aa commit fe6f4c3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Intl.NumberFormat(locales, options)
- `"auto"`: sign display for negative numbers only, including negative zero.
- `"always"`: always display sign.
- `"exceptZero"`: sign display for positive and negative numbers, but not zero.
- `"negative"`: sign display for negative numbers only, excluding negative zero. {{experimental_inline}}
- `"negative"`: sign display for negative numbers only, excluding negative zero.
- `"never"`: never display sign.

- `style`
Expand All @@ -116,7 +116,7 @@ Intl.NumberFormat(locales, options)
- `"short"` (e.g., `16 l`).
- `"narrow"` (e.g., `16l`).

- `useGrouping` {{experimental_inline}}
- `useGrouping`

- : Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. The default is `auto`.

Expand All @@ -126,7 +126,7 @@ Intl.NumberFormat(locales, options)
- `"min2"`: display grouping separators when there are at least 2 digits in a group.
- `true`: alias for `always`.

- `roundingMode` {{experimental_inline}}
- `roundingMode`

- : Options for rounding modes. The default is `halfExpand`.

Expand Down Expand Up @@ -163,7 +163,7 @@ Intl.NumberFormat(locales, options)
These options reflect the [ICU user guide](https://unicode-org.github.io/icu/userguide/format_parse/numbers/rounding-modes.html), where "expand" and "trunc" map to ICU "UP" and "DOWN", respectively.
The [rounding modes](#rounding_modes) example below demonstrates how each mode works.

- `roundingPriority` {{experimental_inline}}
- `roundingPriority`

- : Specify how rounding conflicts will be resolved if both "FractionDigits" ([`minimumFractionDigits`](#minimumfractiondigits)/[`maximumFractionDigits`](#maximumfractiondigits)) and "SignificantDigits" ([`minimumSignificantDigits`](#minimumsignificantdigits)/[`maximumSignificantDigits`](#maximumsignificantdigits)) are specified:

Expand All @@ -173,7 +173,7 @@ Intl.NumberFormat(locales, options)

Note that for values other than `auto` the result with more precision is calculated from the [`maximumSignificantDigits`](#minimumsignificantdigits) and [`maximumFractionDigits`](#maximumfractiondigits) (minimum fractional and significant digit settings are ignored).

- `roundingIncrement` {{experimental_inline}}
- `roundingIncrement`

- : Specifies the rounding-increment precision.
Must be one of the following integers:
Expand Down Expand Up @@ -201,7 +201,7 @@ Intl.NumberFormat(locales, options)
>
> If you set `minimumFractionDigits` and `maximumFractionDigits`, they must set them to the same value; otherwise a `RangeError` is thrown.
- `trailingZeroDisplay` {{experimental_inline}}
- `trailingZeroDisplay`
- : A string expressing the strategy for displaying trailing zeros on whole numbers.
The default is `"auto"`.
Expand Down Expand Up @@ -267,6 +267,21 @@ Note that there's only one actual `Intl.NumberFormat` instance here: the one hid

This behavior, called `ChainNumberFormat`, does not happen when `Intl.NumberFormat()` is called without `new` but with `this` set to anything else that's not an `instanceof Intl.NumberFormat`. If you call it directly as `Intl.NumberFormat()`, the `this` value is [`Intl`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl), and a new `Intl.NumberFormat` instance is created normally.

### Exceptions

- {{jsxref("RangeError")}}

- : Thrown in the following cases:

- A property that takes enumerated values (such as `style`, `units`, `currency`, and so on) is set to an invalid value.
- Both `maximumFractionDigits` and `minimumFractionDigits` are set, and they are set to different values.
Note that depending various formatting options, these properties can have default values.
It is therefore possible to get this error even if you only set one of the properties.

- {{jsxref("TypeError")}}

- : Thrown if the `options.style` property is set to "unit" or "currency", and no value has been set for the corresponding property `options.unit` or `options.currency`.

## Examples

### Basic usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,59 @@ A new object with properties reflecting the [locale and number formatting option

The resulting object has the following properties:

- `compactDisplay`
- : Whether to use short or long form when using compact notation.
This is the value provided in the [`options.compactDisplay`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#compactdisplay) argument of the constructor, or the default value: `"short"`.
The value is only present if `notation` is set to "compact", and otherwise is `undefined`.
- `currency`
- : The currency to use in currency formatting.
The value is defined if `style` is `"currency"`, and is otherwise `undefined`.
This is the value provided in the [`options.currency`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currency) argument of the constructor.
- `currencyDisplay`
- : The display format for the currency, such as a symbol, or currency code.
The value is defined if `style` is `"currency"`, and otherwise is `undefined`.
This is the value provided in the [`options.currencyDisplay`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currencydisplay) argument of the constructor, or the default value: `"symbol"`.
- `currencySign`
- : The method used to specify the sign of the currency value: `standard` or `accounting`.
The value is present if `style` is `"currency"`, and otherwise is `undefined`.
This is the value provided in the [`options.currencySign`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#currencysign) argument of the constructor, or the default value: `"standard"`.
- `locale`
- : The BCP 47 language tag for the locale actually used.
If any Unicode extension values were requested in the input BCP 47 language tag that led to this locale, the key-value pairs that were requested and are supported for this locale are included in `locale`.
- `numberingSystem`
- : The value provided for this properties in the `options` argument, if present, or the value requested using the Unicode extension key `"nu"` or filled in as a default.
- : The BCP 47 language tag for the locale that was actually used.
The key-value pairs that were requested in the constructor [`locale`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#local) and are supported for this locale are included.
- `notation`
- : The value provided for this property in the `options` argument, if present, or `"standard"` filled in as a default.
- `compactDisplay`
- : The value provided for this property in the `options` argument, or `"short"` filled in as a default.
This property is only present if the `notation` is set to "compact".
- : The formatting that should be applied to the number, such as `standard` or `engineering`.
This is the value provided in the [`options.notation`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#notation) argument of the constructor, or the default value: `"standard"`.
- `numberingSystem`
- : The numbering system.
This is the value provided in the [`options.numberingSystem`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#numberingsystem) argument of the constructor, if present, or the value set using the Unicode extension key [`nu`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#nu), or filled in as a default.
- `roundingMode`
- : The rounding mode.
This is the value provided for the [`options.roundingMode`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingmode) argument in the constructor, or the default value: `halfExpand`.
- `roundingPriority`
- : The priority for resolving rounding conflicts if both "FractionDigits" and "SignificantDigits" are specified.
This is the value provided for the [`options.roundingPriority`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingpriority) argument in the constructor, or the default value: `auto`.
- `roundingIncrement`
- : The rounding-increment precision (the increment used when rounding numbers).
This is the value specified in the [`options.roundingIncrement`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingincrement) argument in the constructor.
- `signDisplay`
- : The value provided for this property in the `options` argument, or `"auto"` filled in as a default.
- : Whether or not to display the positive/negative sign.
This is the value specified in the [`options.signDisplay`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#signdisplay) argument in the constructor, or the default value: `"auto"`.
- `unit`
- : The unit to use in unit formatting.
The value is only present if `style` is `"unit"`, and is otherwise `undefined`.
This is the value specified in the [`options.unit`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#unit) argument in the constructor.
- `unitDisplay`

- : The display format to use for units in unit formatting, such as "long", "short" or "narrow".
The value is only present if `style` is `"unit"`, and is otherwise `undefined`.
This is the value specified in the [`options.unitDisplay`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#unitDisplay) argument in the constructor, or the default value: `short`.

- `useGrouping`
- : The value provided for the [useGrouping](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#usegrouping) property in the constructor `options` argument or the default value (`"auto"`).
- `currency`, `currencyDisplay`
- : The values provided for these properties in the `options` argument or filled in as defaults.
These properties are only present if `style` is `"currency"`.
- : Whether or not to use grouping separators to indicate "thousands", "millions" and son on.
This is the value specified in the [`options.useGrouping`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#usegrouping) argument in the constructor, or the default value: `"auto"`.
- `trailingZeroDisplay`
- : The strategy for displaying trailing zeros on whole numbers.
This is the value specified in the [`options.trailingZeroDisplay`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#trailingzerodisplay) argument in the constructor, or the default value: `"auto"`.

Only one of the following two groups of properties is included:

Expand All @@ -55,18 +91,36 @@ Only one of the following two groups of properties is included:
### Using the `resolvedOptions` method

```js
const de = new Intl.NumberFormat("de-DE");
// Create a NumberFormat
const de = new Intl.NumberFormat("de-DE", {
style: "currency",
currency: "USD",
maximumFractionDigits: 2,
roundingIncrement: 5,
roundingMode: "halfCeil",
});

// Resolve the options
const usedOptions = de.resolvedOptions();

usedOptions.locale; // "de-DE"
usedOptions.numberingSystem; // "latn"
usedOptions.notation; // "standard"
usedOptions.signDisplay; // "auto"
usedOptions.style; // "decimal"
usedOptions.minimumIntegerDigits; // 1
usedOptions.minimumFractionDigits; // 0
usedOptions.maximumFractionDigits; // 3
usedOptions.useGrouping; // true
console.log(usedOptions.locale); // "de-DE"
console.log(usedOptions.numberingSystem); // "latn"
console.log(usedOptions.compactDisplay); // undefined ("notation" not set to "compact")
console.log(usedOptions.currency); // "USD"
console.log(usedOptions.currencyDisplay); // "symbol"
console.log(usedOptions.currencySign); // "standard"
console.log(usedOptions.minimumIntegerDigits); // 1
console.log(usedOptions.minimumFractionDigits); // 2
console.log(usedOptions.maximumFractionDigits); // 2
console.log(usedOptions.minimumSignificantDigits); // undefined (maximumFractionDigits is set)
console.log(usedOptions.maximumSignificantDigits); // undefined (maximumFractionDigits is set)
console.log(usedOptions.notation); // "standard"
console.log(usedOptions.roundingIncrement); // 5
console.log(usedOptions.roundingMode); // halfCeil
console.log(usedOptions.roundingPriority); // auto
console.log(usedOptions.signDisplay); // "auto"
console.log(usedOptions.style); // "currency"
console.log(usedOptions.trailingZeroDisplay); // auto
console.log(usedOptions.useGrouping); // auto
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ selectRange(startRange, endRange)
### Return value

A string representing the pluralization category of the `number`.
This can be one of `zero`, `one`, `two`, `few`, `many` or `other`, that are relevant for the locale whose localization is specified in [LDML Language Plural Rules](https://unicode-org.github.io/cldr-staging/charts/37/supplemental/language_plural_rules.html#rules).
This can be one of `zero`, `one`, `two`, `few`, `many` or `other`, that are relevant for the locale whose localization is specified in [LDML Language Plural Rules](https://www.unicode.org/cldr/charts/43/supplemental/language_plural_rules.html).

## Description

Expand Down

0 comments on commit fe6f4c3

Please sign in to comment.