Intl.NumberFormat.prototype.resolvedOptions()
Intl.NumberFormat 实例的 resolvedOptions() 方法返回一个新对象,其属性反映了在此 Intl.NumberFormat 对象初始化期间计算的 区域设置和数字格式选项。
¥The resolvedOptions() method of Intl.NumberFormat instances returns a new object with properties reflecting the locale and number formatting options computed during initialization of this Intl.NumberFormat object.
Try it
语法
参数
返回值
¥Return value
一个新对象,其属性反映在构造给定 Intl.NumberFormat 对象期间计算的 区域设置和数字格式选项。
¥A new object with properties reflecting the locale and number formatting options computed during the construction of the given Intl.NumberFormat object.
生成的对象具有以下属性:
¥The resulting object has the following properties:
compactDisplay-
使用紧凑表示法时是否使用短形式或长形式。这是构造函数的
options.compactDisplay参数中提供的值,或者默认值:"short"。仅当notation设置为 "compact" 时,该值才存在,否则为undefined。 currency-
货币格式中使用的货币。如果
style是"currency",则定义该值,否则为undefined。这是构造函数的options.currency参数中提供的值。 currencyDisplay-
货币的显示格式,例如符号或货币代码。如果
style为"currency",则定义该值,否则为undefined。这是构造函数的options.currencyDisplay参数中提供的值,或者默认值:"symbol"。 currencySign-
用于指定货币值符号的方法:
standard或accounting。如果style为"currency",则该值存在,否则为undefined。这是构造函数的options.currencySign参数中提供的值,或者默认值:"standard"。 locale-
实际使用的区域设置的 BCP 47 语言标记。匹配构造函数
locales中请求的语言环境之一。 notation-
应应用于数字的格式,例如
standard或engineering。这是构造函数的options.notation参数中提供的值,或者默认值:"standard"。 numberingSystem-
编号系统。这是构造函数的
options.numberingSystem参数中提供的值(如果存在),或者使用 Unicode 扩展键nu设置的值,或者作为默认值填充。 roundingIncrement-
舍入增量精度(舍入数字时使用的增量)。这是构造函数中
options.roundingIncrement参数中指定的值。 roundingMode-
舍入模式。这是为构造函数中的
options.roundingMode参数提供的值,或者默认值:halfExpand。 roundingPriority-
如果同时指定了 "FractionDigits" 和 "SignificantDigits",则解决舍入冲突的优先级。这是为构造函数中的
options.roundingPriority参数提供的值,或者默认值:auto。 signDisplay-
是否显示正号/负号。这是构造函数中
options.signDisplay参数中指定的值,或者默认值:"auto"。 unit-
单位格式化中使用的单位。仅当
style为"unit"时,该值才存在,否则为undefined。这是构造函数中options.unit参数中指定的值。 unitDisplay-
单位格式中单位使用的显示格式,例如 "long"、"short" 或 "narrow"。仅当
style为"unit"时,该值才存在,否则为undefined。这是构造函数中options.unitDisplay参数中指定的值,或者默认值:short。 useGrouping-
是否使用分组分隔符来指示 "thousands"、"millions" 及其子。这是构造函数中
options.useGrouping参数中指定的值,或者默认值:"auto"。 trailingZeroDisplay-
在整数上显示尾随零的策略。这是构造函数中
options.trailingZeroDisplay参数中指定的值,或者默认值:"auto"。
仅包含以下两组属性之一:
¥Only one of the following two groups of properties is included:
minimumIntegerDigits,minimumFractionDigits,maximumFractionDigits-
在
options参数中为这些属性提供的值或填写为默认值。仅当options参数中既没有提供minimumSignificantDigits也没有提供maximumSignificantDigits时,这些属性才会出现。 minimumSignificantDigits、maximumSignificantDigits-
在
options参数中为这些属性提供的值或填写为默认值。仅当options参数中至少提供了其中一个属性时,这些属性才会出现。
示例
使用 resolvedOptions 方法
¥Using the resolvedOptions method
// 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();
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
规范
| Specification |
|---|
| ECMAScript Internationalization API Specification # sec-intl.numberformat.prototype.resolvedoptions |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also