BigInt.prototype.toLocaleString()
toLocaleString() 方法用於 BigInt 值,返回一個具有語言敏感表示形式的 BigInt 字串。在支援 Intl.NumberFormat API 的實現中,此方法會委託給 Intl.NumberFormat。
每次呼叫 toLocaleString 時,都需要在大型本地化字串資料庫中進行搜尋,這可能會效率低下。當方法使用相同的引數多次呼叫時,最好建立一個 Intl.NumberFormat 物件並使用其 format() 方法,因為 NumberFormat 物件會記住傳遞給它的引數,並可能決定快取資料庫的一個片段,以便將來的 format 呼叫可以在更受限的上下文中搜索本地化字串。
試一試
const bigint = 123456789123456789n;
// German uses period for thousands
console.log(bigint.toLocaleString("de-DE"));
// Expected output: "123.456.789.123.456.789"
// Request a currency format
console.log(
bigint.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// Expected output: "123.456.789.123.456.789,00 €"
語法
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
引數
locales 和 options 引數自定義函式行為,並允許應用程式指定應使用的語言的格式約定。
在支援 Intl.NumberFormat API 的實現中,這些引數與 Intl.NumberFormat() 建構函式的引數完全對應。不支援 Intl.NumberFormat 的實現會忽略這兩個引數,使得使用的區域設定和返回的字串格式完全取決於實現。
locales可選-
一個包含 BCP 47 語言標籤的字串,或此類字串的陣列。對應於
Intl.NumberFormat()建構函式的locales引數。在不支援
Intl.NumberFormat的實現中,此引數將被忽略,通常會使用主機的區域設定。 options可選-
一個調整輸出格式的物件。對應於
Intl.NumberFormat()建構函式的options引數。在不支援
Intl.NumberFormat的實現中,此引數將被忽略。
有關這些引數的詳細資訊以及如何使用它們,請參閱 Intl.NumberFormat() 建構函式。
返回值
一個根據特定語言約定表示給定 BigInt 的字串。
在支援 Intl.NumberFormat 的實現中,這等同於 new Intl.NumberFormat(locales, options).format(number)。
注意: 大多數情況下,toLocaleString() 返回的格式是一致的。然而,輸出在不同實現之間可能會有所不同,即使在相同的區域設定中也是如此——輸出差異是設計使然,並受規範允許。它也可能不符合您的預期。例如,字串可能使用不間斷空格或被雙向控制字元包圍。您不應將 toLocaleString() 的結果與硬編碼常量進行比較。
示例
使用 toLocaleString()
不指定 locale 的此方法的簡單用法以預設區域設定和預設選項返回格式化字串。
const bigint = 3500n;
console.log(bigint.toLocaleString());
// "3,500" if in U.S. English locale
檢查對 locales 和 options 引數的支援
locales 和 options 引數可能並非在所有實現中都得到支援,因為對國際化 API 的支援是可選的,並且某些系統可能沒有必要的資料。對於不支援國際化的實現,toLocaleString() 始終使用系統的語言環境,這可能不是您想要的。由於任何支援 locales 和 options 引數的實現都必須支援 Intl API,因此您可以檢查後者是否存在來判斷是否支援。
function toLocaleStringSupportsLocales() {
return (
typeof Intl === "object" &&
!!Intl &&
typeof Intl.NumberFormat === "function"
);
}
使用語言環境
此示例展示了本地化數字格式的一些變化。為了獲得應用程式使用者介面中使用的語言的格式,請務必使用 locales 引數指定該語言(以及可能的備用語言)。
const bigint = 123456789123456789n;
// German uses period for thousands
console.log(bigint.toLocaleString("de-DE"));
// 123.456.789.123.456.789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(bigint.toLocaleString("ar-EG"));
// ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩
// India uses thousands/lakh/crore separators
console.log(bigint.toLocaleString("en-IN"));
// 1,23,45,67,89,12,34,56,789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(bigint.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// 一二三,四五六,七八九,一二三,四五六,七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(bigint.toLocaleString(["ban", "id"]));
// 123.456.789.123.456.789
使用選項
toLocaleString() 提供的結果可以使用 options 引數進行自定義。
const bigint = 123456789123456789n;
// request a currency format
console.log(
bigint.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// 123.456.789.123.456.789,00 €
// the Japanese yen doesn't use a minor unit
console.log(
bigint.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }),
);
// ¥123,456,789,123,456,789
// limit to three significant digits
console.log(bigint.toLocaleString("en-IN", { maximumSignificantDigits: 3 }));
// 1,23,00,00,00,00,00,00,000
規範
| 規範 |
|---|
| ECMAScript® 2026 國際化 API 規範 # sup-bigint.prototype.tolocalestring |
瀏覽器相容性
載入中…