您可能已經熟悉 Intl.NumberFormat
API,因為它已經在現代環境中支援了一段時間。
- Chrome: 自 24 版起支援
- Firefox: 自 29 版起支援
- Safari: 自 10 版起支援
- Node.js: 自 0.12 版起支援
- Babel: 支援
在最基本的型式中,Intl.NumberFormat
讓您可以建立一個可重複使用的格式化器執行個體,支援與語言環境相關的數字格式化。就像其他 Intl.*Format
API 一樣,一個格式化器執行個體支援 format
和 formatToParts
方法
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' }
// → ]
注意:雖然許多 Intl.NumberFormat
功能可以使用 Number.prototype.toLocaleString
達成,但 Intl.NumberFormat
通常是更好的選擇,因為它能建立一個可重複使用的格式化器執行個體,而且往往 更有效率。
最近,Intl.NumberFormat
API 獲得了一些新功能。
BigInt
支援 #
除了 Number
之外,Intl.NumberFormat
現在也可以格式化 BigInt
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' }
// → ]
- Chrome: 自 76 版起支援
- Firefox: 不支援
- Safari: 不支援
- Node.js: 不支援
- Babel: 不支援
測量單位 #
Intl.NumberFormat
目前支援以下所謂的簡單單位
- 角度:
度數
- 面積:
英畝
、公頃
- 濃度:
百分比
- 數位:
位元
、位元組
、千位元
、千位元組
、百萬位元
、百萬位元組
、十億位元
、十億位元組
、兆位元
、兆位元組
、京位元
- 時間:
毫秒
、秒
、分鐘
、小時
、天
、週
、月
、年
- 長度:
公釐
、公分
、公尺
、公里
、英吋
、英尺
、碼
、英里
、斯堪地那維亞英里
- 質量:
公克
、公斤
、盎司
、磅
、英石
- 溫度:
攝氏
、華氏
- 體積:
公升
、毫升
、加侖
、液體盎司
若要使用在地化單位格式化數字,請使用 style
和 unit
選項
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'kilobyte',
});
formatter.format(1.234);
// → '1.234 kB'
formatter.format(123.4);
// → '123.4 kB'
請注意,隨著時間推移,可能會新增對更多單位的支援。請參閱規格,取得最新的最新清單。
上述簡單單位可以組合成任意分子和分母對,以表示複合單位,例如「每英畝公升」或「每秒公尺」
const formatter = new Intl.NumberFormat('en', {
style: 'unit',
unit: 'meter-per-second',
});
formatter.format(299792458);
// → '299,792,458 m/s'
- Chrome: 自 77 版起支援
- Firefox: 不支援
- Safari: 不支援
- Node.js: 不支援
- Babel: 不支援
精簡、科學和工程記號 #
精簡記號使用特定地區的符號來表示大數字。它是科學記號更人性化的替代方案
{
// Test standard notation.
const formatter = new Intl.NumberFormat('en', {
notation: 'standard', // This is the implied default.
});
formatter.format(1234.56);
// → '1,234.56'
formatter.format(123456);
// → '123,456'
formatter.format(123456789);
// → '123,456,789'
}
{
// Test compact notation.
const formatter = new Intl.NumberFormat('en', {
notation: 'compact',
});
formatter.format(1234.56);
// → '1.2K'
formatter.format(123456);
// → '123K'
formatter.format(123456789);
// → '123M'
}
注意:預設情況下,精簡記號會四捨五入到最接近的整數,但始終保留 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'
- Chrome: 自 77 版起支援
- Firefox: 不支援
- Safari: 不支援
- Node.js: 不支援
- Babel: 不支援
符號顯示 #
在某些情況下(例如呈現增量),即使數字為正數,也能明確顯示符號。新的 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 still displays with a sign, as you’d expect:
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)'
- Chrome: 自 77 版起支援
- Firefox: 不支援
- Safari: 不支援
- Node.js: 不支援
- Babel: 不支援
更多資訊 #
相關的規格建議有更多資訊和範例,包括如何針對每個個別 Intl.NumberFormat
功能進行功能偵測的指南。