Intl.Numberformat.prototype.formatToParts()
method allows locale-aware formatting of strings produced by
NumberFormat
formatters.
Intl.NumberFormat.prototype.formatToParts(number)
Array
of objects containing the formatted number in parts.
formatToParts()
method is useful for custom formatting of number strings. It returns an
Array
of objects containing the locale-specific tokens from which it possible to build custom strings while preserving the locale-specific parts. The structure the
formatToParts()
method returns, looks like this:
[
{ type: "integer", value: "3" },
{ type: "group", value: "." },
{ type: "integer", value: "500" }
]
Possible types are the following:
currencyDisplay
被指定。
The decimal separator string (".").
The fraction number.
The group separator string (",").
Infinity
string ("∞").
The integer number.
Any literal strings or whitespace in the formatted number.
The minus sign string ("-").
NaN
string ("NaN").
The plus sign string ("+").
The percent sign string ("%").
NumberFormat
outputs localized, opaque strings that cannot be manipulated directly:
var number = 3500;
var formatter = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
});
formatter.format(number);
// "3.500,00 €"
However, in many User Interfaces there is a desire to customize the formatting of this string. The
formatToParts
method enables locale-aware formatting of strings produced by
NumberFormat
formatters by providing you the string in parts:
formatter.formatToParts(number);
// return value:
[
{ type: "integer", value: "3" },
{ type: "group", value: "." },
{ type: "integer", value: "500" },
{ type: "decimal", value: "," },
{ type: "fraction", value: "00" },
{ type: "literal", value: " " },
{ type: "currency", value: "€" }
]
Now the information is available separately and it can be formatted and concatenated again in a customized way. For example by using
Array.prototype.map()
,
箭头函数
,
switch statement
,
template literals
,和
Array.prototype.reduce()
.
var numberString = formatter.formatToParts(number).map(({type, value}) => {
switch (type) {
case 'currency': return `<strong>${value}</strong>`;
default : return value;
}
}).reduce((string, part) => string + part);
This will make the currency bold, when using the
formatToParts()
方法。
console.log(numberString); // "3.500,00 <strong>€</strong>"
| 规范 |
|---|
|
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.NumberFormat.prototype.formatToParts' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
formatToParts
|
Chrome 64 | Edge 12 | Firefox 58 | IE No | Opera 51 | Safari 13 | WebView Android 64 | Chrome Android 64 | Firefox Android 58 | Opera Android 47 | Safari iOS 13 | Samsung Internet Android 9.0 |
nodejs
10.0.0
|
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
Intl.NumberFormat
Intl.NumberFormat.prototype.format
Intl.DateTimeFormat.prototype.formatToParts()
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()