Intl.NumberFormat
Baseline 廣泛可用 *
Intl.NumberFormat 物件支援進行區域語言敏感的數字格式化。
試一試
const number = 123456.789;
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// Expected output: "123.456,79 €"
// The Japanese yen doesn't use a minor unit
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// Expected output: "¥123,457"
// Limit to three significant digits
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// Expected output: "1,23,000"
建構函式
Intl.NumberFormat()-
建立一個新的
NumberFormat物件。
靜態方法
Intl.NumberFormat.supportedLocalesOf()-
返回一個數組,其中包含提供的區域設定中受支援的那些區域設定,而無需回退到執行時預設區域設定。
例項屬性
這些屬性定義在 Intl.NumberFormat.prototype 上,並被所有 Intl.NumberFormat 例項共享。
Intl.NumberFormat.prototype.constructor-
建立了例項物件的建構函式。對於
Intl.NumberFormat例項,初始值為Intl.NumberFormat建構函式。 Intl.NumberFormat.prototype[Symbol.toStringTag]-
[Symbol.toStringTag]屬性的初始值為字串"Intl.NumberFormat"。此屬性用於Object.prototype.toString()。
例項方法
Intl.NumberFormat.prototype.format()-
getter 函式,根據此
Intl.NumberFormat物件的區域語言和格式化選項格式化數字。 Intl.NumberFormat.prototype.formatRange()-
getter 函式,根據呼叫該方法的
Intl.NumberFormat物件的區域語言和格式化選項格式化數字範圍。 Intl.NumberFormat.prototype.formatRangeToParts()-
返回一個表示數字範圍字串的物件的
Array,這些部分可用於自定義區域語言感知格式化。 Intl.NumberFormat.prototype.formatToParts()-
返回一個表示數字字串的物件的
Array,這些部分可用於自定義區域語言感知格式化。 Intl.NumberFormat.prototype.resolvedOptions()-
返回一個新的物件,其中包含在物件初始化期間計算出的區域設定和排序選項。
示例
基本用法
在不指定區域設定的基本使用中,將返回採用預設區域設定和預設選項的格式化字串。
js
const number = 3500;
console.log(new Intl.NumberFormat().format(number));
// '3,500' if in US English locale
使用語言環境
此示例展示了本地化數字格式的一些變化。為了獲得應用程式使用者介面中使用的語言的格式,請務必使用 locales 引數指定該語言(以及可能的備用語言)。
js
const number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(new Intl.NumberFormat("de-DE").format(number));
// 123.456,789
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(new Intl.NumberFormat("ar-EG").format(number));
// ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(new Intl.NumberFormat("en-IN").format(number));
// 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(number));
// 一二三,四五六.七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(new Intl.NumberFormat(["ban", "id"]).format(number));
// 123.456,789
使用選項
可以使用 options 引數自定義結果
js
const number = 123456.789;
// request a currency format
console.log(
new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
number,
),
);
// 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
number,
),
);
// ¥123,457
// limit to three significant digits
console.log(
new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
number,
),
);
// 1,23,000
// Formatting with units
console.log(
new Intl.NumberFormat("pt-PT", {
style: "unit",
unit: "kilometer-per-hour",
}).format(50),
);
// 50 km/h
console.log(
(16).toLocaleString("en-GB", {
style: "unit",
unit: "liter",
unitDisplay: "long",
}),
);
// 16 litres
有關選項的詳盡列表,請參閱 Intl.NumberFormat() 建構函式頁面。
規範
| 規範 |
|---|
| ECMAScript® 2026 國際化 API 規範 # numberformat-objects |
瀏覽器相容性
載入中…