BigInt.prototype.toLocaleString()

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上使用。自 2020 年 9 月起,所有浏览器均已提供此功能。

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 €"

语法

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() 构造函数

返回值

一个根据特定语言约定表示给定 BigInt 的字符串。

在支持 Intl.NumberFormat 的实现中,这等同于 new Intl.NumberFormat(locales, options).format(number)

注意: 大多数情况下,toLocaleString() 返回的格式是一致的。然而,输出在不同实现之间可能会有所不同,即使在相同的区域设置中也是如此——输出差异是设计使然,并受规范允许。它也可能不符合您的预期。例如,字符串可能使用不间断空格或被双向控制字符包围。您不应将 toLocaleString() 的结果与硬编码常量进行比较。

示例

使用 toLocaleString()

不指定 locale 的此方法的简单用法以默认区域设置和默认选项返回格式化字符串。

js
const bigint = 3500n;

console.log(bigint.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 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 参数进行自定义。

js
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

浏览器兼容性

另见