大网数据广告活动
Web 标准与前端开发

BigInt.prototype.toLocaleString()

· 52CE 我爱测

**`toLocaleString()`** 方法返回一个表示给定 BigInt 对象的字符串,该字符串格式因不同语言而不同。在支持 `Intl.NumberFormat` API 的实现中,该方法仅是调用了 `Intl.NumberFormat` 方法。

toLocaleString() 方法返回一个表示给定 BigInt 对象的字符串,该字符串格式因不同语言而不同。在支持 Intl.NumberFormat API 的实现中,该方法仅是调用了 Intl.NumberFormat 方法。

JavaScript Demo: BigInt.toLocaleString()

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 参数使程序能够指定使用哪种语言格式化规则,允许定制该方法的行为(behavior)。

在支持 Intl.NumberFormat API 的实现中,这些参数与 Intl.NumberFormat() 构造函数的参数完全对应。而对于不支持 Intl.NumberFormat 的实现,则要求函数忽略这两个参数,使得函数使用的区域(locale)以及返回的字符串的格式完全取决于实现。

  • locales

    • : 表示缩写语言代码(BCP 47 language tag)的字符串,或由此类字符串组成的数组。对应于 Intl.NumberFormat() 构造函数的 locales 参数。

      在不支持 Intl.NumberFormat 的实现中,该参数会被忽略,并且通常会使用主机的区域设置。

  • options

    • : 一个调整输出格式的对象。对应于 Intl.NumberFormat() 构造函数的 options 参数。

      在不支持 Intl.NumberFormat 的实现中,该参数会被忽略。

参见 Intl.NumberFormat() 构造函数以详细了解这些参数以及如何使用它们。

返回值

一个表示给定 BitInt 对象的字符串,该字符串格式因不同语言而不同。

在支持 Intl.NumberFormat 的实现中,此方法等价于 new Intl.NumberFormat(locales, options).format(number)。

性能

当需要对大量的数字进行格式化时,最好创建一个 Intl.NumberFormat 对象,并使用其 Intl/NumberFormat/format 方法提供的函数。

示例

使用 toLocaleString

没有指定区域(locale)时,返回一个使用默认区域和格式设置(options)的格式化字符串。

const bigint = 3500n;

console.log(bigint.toLocaleString());
// "3,500" 如果区域为 en-US

使用 locales

这个例子展示了本地化数字格式的一些变体。为了获得应用程序用户界面中使用的语言的格式,请确保使用 locales 参数指定该语言(可能还有一些回退语言):

const bigint = 123456789123456789n;

// 德国使用句号表示千位
console.log(bigint.toLocaleString("de-DE"));
// 123.456.789.123.456.789

// 大多数说阿拉伯语的阿拉伯国家使用东阿拉伯数字
console.log(bigint.toLocaleString("ar-EG"));
// ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩

// 印度使用千、万、千万分隔符
console.log(bigint.toLocaleString("en-IN"));
// 1,23,45,67,89,12,34,56,789

// nu 扩展键要求使用编号系统,例如中文十进制数
console.log(bigint.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// 一二三,四五六,七八九,一二三,四五六,七八九

// 当使用的语言不被支持,例如
// 巴厘岛语,则可以包含一种回退语言,这里以印尼语为例
console.log(bigint.toLocaleString(["ban", "id"]));
// 123.456.789.123.456.789

使用 options

toLocaleString 提供的结果可以使用 options 参数进行自定义:

const bigint = 123456789123456789n;

// 要求使用货币格式
console.log(
  bigint.toLocaleString("de-DE", { style: "currency", currency: "EUR" }),
);
// 123.456.789.123.456.789,00 €

// 日元不使用小数单位
console.log(
  bigint.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }),
);
// ¥123,456,789,123,456,789

// 保留三位有效数字
console.log(bigint.toLocaleString("en-IN", { maximumSignificantDigits: 3 }));
// 1,23,00,00,00,00,00,00,000

规范

浏览器兼容性

参见

  • BigInt.prototype.toString()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。