JavaScript - TypedArray 的 toLocaleString() 方法



JavaScript TypedArray 的 toLocaleString() 方法返回当前 TypedArray 元素的字符串表示形式。此方法将 TypedArray 元素转换为字符串,并使用指定的特定于区域设置的分隔符(例如逗号 (",")、符号 ($, ₹) 等)分隔这些字符串。

指定的区域设置参数是BCP (最佳当前实践) 47 语言标签,是区域化语言表示的标准。

语法

以下是 JavaScript TypedArray toLocaleString() 方法的语法:

toLocaleString(locales, options)

参数

此方法接受两个名为“locales”和“options”的参数,如下所述:

  • locales − 它是一个带有 BCP 47 语言标签的字符串,指定语言和格式选项。

  • options − 对象用作选项,用于更改样式、货币、最小和最大小数位数等。

返回值

此方法返回当前 TypedArray 元素的字符串表示形式。

示例

示例 1

如果省略localesoptions 参数,它会将当前 TypedArray [1000, 205, 5993, 4032] 的元素转换为字符串(考虑您的系统区域设置),并用逗号(',')分隔这些字符串。

<html>
<head>
   <title>JavaScript TypedArray toLocaleString() Method</title>
</head>
<body>
   <script>
      const T_array = new Int16Array([1000, 205, 5993, 4032]);
      document.write("Typed array: ", T_array);
      
      //using toLocaleString() method
      let str = T_array.toLocaleString();
      document.write("<br>The string representing typed array elements: ", str);
   </script>
</body>
</html>

输出

上面的程序返回一个表示 TypedArray 元素的字符串:

Typed array: 1000,205,5993,4032
The string representing typed array elements: 1,000,205,5,993,4,032

示例 2

如果我们只将"locale" 参数传递给此方法,它会根据指定的区域设置将 TypedArray 元素转换为字符串。

以下是 JavaScript TypedArray toLocaleString() 方法的另一个示例。我们使用此方法根据指定的区域设置"de-DE"检索此 TypedArray [4023, 6123, 30, 146] 元素的字符串表示形式。

<html>
<head>
   <title>JavaScript TypedArray toLocaleString() Method</title>
</head>
<body>
   <script>
      const T_array = new Int16Array([4023, 6123, 30, 146]);
      document.write("Typed array: ", T_array);
      const locale = "de-DE";
      document.write("<br>The locale: ", locale);
      
      //using toLocaleString() method
      let str = T_array.toLocaleString(locale);
      document.write("<br>The string representing typed array elements: ", str);
   </script>
</body>
</html>

输出

执行上述程序后,它将返回一个表示 TypedArray 元素的字符串:

Typed array: 4023,6123,30,146
The locale: de-DE
The string representing typed array elements: 4.023,6.123,30,146

示例 3

如果我们将localeoptions 参数都传递给此方法,它将根据指定的区域设置和选项(配置货币符号等属性)将 TypedArray 元素转换为字符串。

在下面的示例中,我们使用 JavaScript TypedArray toLocaleString() 方法,使用指定的国家/地区区域设置"en-In"和货币符号"₹"检索此 TypedArray [5034, 6946, 234, 1003] 元素的字符串表示形式。

<html>
<head>
   <title>JavaScript TypedArray toLocaleString() Method</title>
</head>
<body>
   <script>
      const T_array = new Int16Array([5034, 6946, 234, 1003]);
      document.write("Typed array: ", T_array);
      const locale = "en-IN";
      const options = {style: "currency", currency: "INR"};
      document.write("<br>The locale: ", locale);
      document.write("<br>The options: ", options.style,", ",options.currency);
      
      //using toLocaleString() method
      let str = T_array.toLocaleString(locale, options);
      document.write("<br>The string representing typed array elements: ", str);
   </script>
</body>
</html>

输出

执行上述程序后,它将显示如下输出:

Typed array: 5034,6946,234,1003
The locale: en-IN
The options: currency, INR
The string representing typed array elements: ₹5,034.00,₹6,946.00,₹234.00,₹1,003.00
广告
© . All rights reserved.