Intl.NumberFormat.prototype.formatRangeToParts()
Intl.NumberFormat 实例的 formatRangeToParts() 方法返回包含特定于区域设置的标记的 Array 个对象,可以从中构建自定义字符串,同时保留特定于区域设置的部分。这使得提供区域设置感知的数字字符串自定义格式范围成为可能。
¥The formatRangeToParts() method of Intl.NumberFormat instances returns an Array of objects containing the locale-specific tokens from which it is possible to build custom strings while preserving the locale-specific parts. This makes it possible to provide locale-aware custom formatting ranges of number strings.
语法
参数
返回值
¥Return value
包含格式化数字范围的对象的 Array 个部分。
¥An Array of objects containing the formatted range of numbers in parts.
返回的结构如下所示:
¥The structure of the returned looks like this:
[
{ type: "integer", value: "3", source: "startRange" },
{ type: "literal", value: "-", source: "shared" },
{ type: "integer", value: "5", source: "endRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "currency", value: "€", source: "shared" },
];
type 属性的可能值包括:
¥Possible values for the type property include:
currency-
货币字符串,例如符号 "$" 和 "€" 或名称 "美元"、"欧元",具体取决于
currencyDisplay的指定方式。 decimal-
小数点分隔符字符串 (".")。
fraction-
分数。
group-
组分隔符字符串 (",")。
infinity-
Infinity字符串 ("∞")。 integer-
整数。
literal-
格式化数字中的任何文字字符串或空格。
minusSign-
减号字符串 ("*")。
nan-
NaN字符串 ("NaN")。 plusSign-
加号字符串 ("*")。
percentSign-
百分号字符串 ("%")。
unit-
单位字符串,例如 "l" 或 "litres",具体取决于
unitDisplay的指定方式。
source 属性的可能值包括:
¥Possible values for the source property include:
startRange-
该对象是范围的开始部分。
endRange-
该对象是范围的结束部分。
-
该对象是范围的 "shared" 部分,例如分隔符或货币。
例外情况
¥Exceptions
RangeError-
如果
startRange小于endRange,或者任一值为NaN,则抛出该异常。 TypeError-
如果
startRange或endRange未定义,则抛出该异常。
示例
比较 formatRange 和 formatRangeToParts
¥Comparing formatRange and formatRangeToParts
NumberFormat 输出无法直接操作的本地化、不透明字符串:
¥NumberFormat outputs localized, opaque strings that cannot be manipulated directly:
const startRange = 3500;
const endRange = 9500;
const formatter = new Intl.NumberFormat("de-DE", {
style: "currency",
currency: "EUR",
});
console.log(formatter.formatRange(startRange, endRange));
// "3.500,00–9.500,00 €"
然而,对于许多用户界面来说,需要自定义该字符串的格式。formatRangeToParts 方法通过为你提供部分字符串来启用 NumberFormat 格式化程序生成的字符串的区域设置感知格式:
¥However, for many user interfaces there is a need to customize the formatting of this string.
The formatRangeToParts method enables locale-aware formatting of strings produced by NumberFormat formatters by providing you the string in parts:
console.log(formatter.formatRangeToParts(startRange, endRange));
// return value:
[
{ type: "integer", value: "3", source: "startRange" },
{ type: "group", value: ".", source: "startRange" },
{ type: "integer", value: "500", source: "startRange" },
{ type: "decimal", value: ",", source: "startRange" },
{ type: "fraction", value: "00", source: "startRange" },
{ type: "literal", value: "–", source: "shared" },
{ type: "integer", value: "9", source: "endRange" },
{ type: "group", value: ".", source: "endRange" },
{ type: "integer", value: "500", source: "endRange" },
{ type: "decimal", value: ",", source: "endRange" },
{ type: "fraction", value: "00", source: "endRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "currency", value: "€", source: "shared" },
];
规范
| Specification |
|---|
| ECMAScript Internationalization API Specification # sec-intl.numberformat.prototype.formatrangetoparts |
浏览器兼容性
BCD tables only load in the browser