Number.prototype.toLocaleString()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

toLocaleString() 方法是 Number 物件的一個方法,它返回一個表示該數字的、符合語言習慣的字串。在支援 Intl.NumberFormat API 的實現中,此方法會委託給 Intl.NumberFormat

每次呼叫 toLocaleString 時,都需要在一個龐大的本地化字串資料庫中進行搜尋,這可能會效率低下。當使用相同的引數多次呼叫該方法時,最好建立一個 Intl.NumberFormat 物件並使用其 format() 方法,因為 NumberFormat 物件會記住傳遞給它的引數,並可能決定快取資料庫的一部分,從而使將來的 format 呼叫可以在更受限的上下文中搜索本地化字串。

試一試

function eArabic(x) {
  return x.toLocaleString("ar-EG");
}

console.log(eArabic(123456.789));
// Expected output: "١٢٣٬٤٥٦٫٧٨٩"

console.log(eArabic("123456.789"));
// Expected output: "123456.789"

console.log(eArabic(NaN));
// Expected output: "ليس رقم"

語法

js
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)

引數

localesoptions 引數自定義函式行為,並允許應用程式指定應使用的語言的格式約定。

在支援 Intl.NumberFormat API 的實現中,這些引數與 Intl.NumberFormat() 建構函式的引數完全對應。不支援 Intl.NumberFormat 的實現被要求忽略這兩個引數,因此所使用的區域設定和返回字串的格式完全取決於實現。

locales 可選

一個字串,包含一個 BCP 47 語言標籤,或者這些字串的陣列。對應於 Intl.NumberFormat() 建構函式的 locales 引數。

在不支援 Intl.NumberFormat 的實現中,此引數被忽略,通常會使用主機的區域設定。

options 可選

一個調整輸出格式的物件。對應於 Intl.NumberFormat() 建構函式的 options 引數。

在不支援 Intl.NumberFormat 的實現中,此引數被忽略。

有關這些引數的詳細資訊以及如何使用它們,請參閱 Intl.NumberFormat() 建構函式

返回值

一個字串,根據特定語言的約定表示給定的數字。

在支援 Intl.NumberFormat 的實現中,這等同於 new Intl.NumberFormat(locales, options).format(number)

注意: 大多數情況下,toLocaleString() 返回的格式是一致的。然而,輸出在不同實現之間可能會有所不同,即使在相同的區域設定中也是如此——輸出差異是設計使然,並受規範允許。它也可能不符合您的預期。例如,字串可能使用不間斷空格或被雙向控制字元包圍。您不應將 toLocaleString() 的結果與硬編碼常量進行比較。

示例

使用 toLocaleString()

不指定 locale 的此方法的簡單用法以預設區域設定和預設選項返回格式化字串。

js
const number = 3500;

console.log(number.toLocaleString()); // "3,500" if in U.S. English locale

檢查對 locales 和 options 引數的支援

localesoptions 引數可能並非在所有實現中都得到支援,因為對國際化 API 的支援是可選的,並且某些系統可能沒有必要的資料。對於不支援國際化的實現,toLocaleString() 始終使用系統的語言環境,這可能不是您想要的。由於任何支援 localesoptions 引數的實現都必須支援 Intl API,因此您可以檢查後者是否存在來判斷是否支援。

js
function toLocaleStringSupportsLocales() {
  return (
    typeof Intl === "object" &&
    !!Intl &&
    typeof Intl.NumberFormat === "function"
  );
}

使用語言環境

此示例展示了本地化數字格式的一些變化。為了獲得應用程式使用者介面中使用的語言的格式,請務必使用 locales 引數指定該語言(以及可能的備用語言)。

js
const number = 123456.789;

// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString("de-DE"));
// 123.456,789

// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString("ar-EG"));
// ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
console.log(number.toLocaleString("en-IN"));
// 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.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(number.toLocaleString(["ban", "id"]));
// 123.456,789

使用選項

toLocaleString() 提供的結果可以使用 options 引數進行自定義。

js
const number = 123456.789;

// request a currency format
console.log(
  number.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// 123.456,79 €

// the Japanese yen doesn't use a minor unit
console.log(
  number.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }),
);
// ¥123,457

// limit to three significant digits
console.log(number.toLocaleString("en-IN", { maximumSignificantDigits: 3 }));
// 1,23,000

// Use the host default language with options for number formatting
const num = 30000.65;
console.log(
  num.toLocaleString(undefined, {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }),
);
// "30,000.65" where English is the default language, or
// "30.000,65" where German is the default language, or
// "30 000,65" where French is the default language

規範

規範
ECMAScript® 2026 語言規範
# sec-number.prototype.tolocalestring
ECMAScript® 2026 國際化 API 規範
# sup-number.prototype.tolocalestring

瀏覽器相容性

另見