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
廣告