TypedArray.prototype.toLocaleString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

toLocaleString() 方法返回一个字符串,表示该类型化数组的元素。这些元素被转化为字符串并由一个区域设置指定的分隔符(例如逗号“,”)分隔。这个方法与 Array.prototype.toLocaleString() 拥有相同的算法。同时,由于类型化数组的元素都是数字,将每个元素转化为字符串的算法与 Number.prototype.toLocaleString() 是相同的。类型化数组需要是类型化数组类型中的一种类型。

语法

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

参数

localesoptions 参数用于自定义函数的行为,并让应用程序指定应该使用的格式约定的语言。在忽略 localesoptions 参数的实现中,使用的语言环境和返回的字符串形式完全取决于实现。

参见 Intl.NumberFormat() (en-US) 构造函数,以了解这些参数的详细信息,以及如何使用它们。

返回值

一个字符串,表示该类型化数组内的元素。

示例

使用 toLocaleString

js
const uint = new Uint32Array([2000, 500, 8123, 12, 4212]);

uint.toLocaleString();
// 如果在 de-DE 区域设置下运行
// "2.000,500,8.123,12,4.212"

uint.toLocaleString("en-US");
// "2,000,500,8,123,12,4,212"

uint.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });
// "¥2,000,¥500,¥8,123,¥12,¥4,212"

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.tolocalestring

浏览器兼容性

BCD tables only load in the browser

参见