也许您已经知道 Intl.NumberFormat
API 了,由于它已经在浏览器中获得了一段时间的支持。html
Intl.NumberFormat
最基本的形式是建立一个可复用的格式化实例,该实例支持按区域设置数字格式。与其余 Intl.*Format
API 同样,formatter 实例同时支持 format
和 formatToParts
两个方法:git
const formatter = new Intl.NumberFormat('en');
formatter.format(987654.321);
// → '987,654.321'
formatter.formatToParts(987654.321);
// → [
// → { type: 'integer', value: '987' },
// → { type: 'group', value: ',' },
// → { type: 'integer', value: '654' },
// → { type: 'decimal', value: '.' },
// → { type: 'fraction', value: '321' }
// → ]
复制代码
Note: 虽然 Intl.NumberFormat
的多数功能能够经过 Number.prototype.toLocaleString
来实现,但Intl.NumberFormat
仍然是最好的选择,由于它建立的可复用实例效率更高。github
最近,Intl.NumberFormat
API 新增了一些新功能。浏览器
BigInt
除了 Number
以外,Intl.NumberFormat
如今还能够格式化BigInt
:ide
译者注:
BigInt
是一种内置对象,能够表示大于 2^53 的整数。而在 Javascript 中,Number
基本类型能够精确表示的最大整数是 2^53。BigInt
能够表示任意大的整数。ui
const formatter = new Intl.NumberFormat('fr');
formatter.format(12345678901234567890n);
// → '12 345 678 901 234 567 890'
formatter.formatToParts(123456n);
// → [
// → { type: 'integer', value: '123' },
// → { type: 'group', value: ' ' },
// → { type: 'integer', value: '456' }
// → ]
复制代码
Intl.NumberFormat
目前支持如下简单的单位(simple units):spa
degree
acre
,hectare
percent
bit
,byte
,kilobit
,kilobyte
,megabit
,megabyte
,gigabit
,gigabyte
,terabit
,terabyte
,petabyte
millisecond
,second
,minute
,hour
,day
,week
,month
,year
millimeter
,centimeter
,meter
,kilometer
,inch
,foot
,yard
,mile
,mile-scandinavian
gram
,kilogram
,ounce
,pound
,stone
celsius
,fahrenheit
liter
,milliliter
,gallon
,fluid-ounce
使用 style
和 unit
选项可将数字格式化为本地单位:prototype
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'kilobyte',
});
formatter.format(1.234);
// → '1.234 kB'
formatter.format(123.4);
// → '123.4 kB'
复制代码
Note: 随着时间的推移,可能会支持更多单位。这里是最新的列表。3d
上述单位能够组合成任意的分子/分母对,以表示复合单位,如「liters per acre」或「meters per second」:code
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'meter-per-second',
});
formatter.format(299792458);
// → '299,792,458 m/s'
复制代码
紧密计法是使用特定于语言环境的符号来表示大数字。它是科学计数法的一种更人性化的替代方案:
{
// 标准符号表示法
const formatter = new Intl.NumberFormat('en', {
notation: 'standard', // 默认值
});
formatter.format(1234.56);
// → '1,234.56'
formatter.format(123456);
// → '123,456'
formatter.format(123456789);
// → '123,456,789'
}
{
// 紧密计法
const formatter = new Intl.NumberFormat('en', {
notation: 'compact',
});
formatter.format(1234.56);
// → '1.2K'
formatter.format(123456);
// → '123K'
formatter.format(123456789);
// → '123M'
}
复制代码
Note: 默认状况下,紧密计法会舍入到最接近的整数,但始终会保持 2 位有效数字。您能够设置 {minimum,maximum}FractionDigits
或 {minimum,maximum}SignificantDigits
覆盖该行为。
Intl.NumberFormat
也能够将数字格式化为科学计数法:
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'meter-per-second',
notation: 'scientific',
});
formatter.format(299792458);
// → '2.998E8 m/s'
复制代码
也支持工程计数法:
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'meter-per-second',
notation: 'engineering',
});
formatter.format(299792458);
// → '299.792E6 m/s'
复制代码
在某些状况下,即便数字为正数,也能够明确显示符号。signDisplay
选项的启用以下:
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'percent',
signDisplay: 'always',
});
formatter.format(-12.34);
// → '-12.34%'
formatter.format(12.34);
// → '+12.34%'
formatter.format(0);
// → '+0%'
formatter.format(-0);
// → '-0%'
复制代码
当数值为 0
时不想显示符号,能够设置 signDisplay: 'exceptZero'
:
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'percent',
signDisplay: 'exceptZero',
});
formatter.format(-12.34);
// → '-12.34%'
formatter.format(12.34);
// → '+12.34%'
formatter.format(0);
// → '0%'
// Note: -0 仍然会显示符号
formatter.format(-0);
// → '-0%'
复制代码
对于货币单位,currencySign
选项会启用会计格式,该格式能够将金额为负数的状况格式化成特定区域设置的格式; 好比,将金额包装在括号中:
const formatter = new Intl.NumberFormat('en', {
style: 'currency',
currency: 'USD',
signDisplay: 'exceptZero',
currencySign: 'accounting',
});
formatter.format(-12.34);
// → '($12.34)'
formatter.format(12.34);
// → '+$12.34'
formatter.format(0);
// → '$0.00'
formatter.format(-0);
// → '($0.00)'
复制代码
你能够查看相关规范提案了解更多信息及查看更多示例,同时还包括如何检测每一个 Intl.NumberFormat
功能的指导。
参考:BigInt MDN
译者:Mark Wong