aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/bigint/tolocalestring/index.html
blob: 954db703f20c177c8d0d117a8d4d6dfed14cae92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: BigInt.prototype.toLocaleString()
slug: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString
translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString
---
<div>{{JSRef}}</div>

<p><strong><code>toLocaleString()</code></strong> 方法返回一个字符串,该字符串具有此 <code>BigInt</code> 的 language-sensitive 表达形式。</p>

<div>{{EmbedInteractiveExample("pages/js/bigint-tolocalestring.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox"><code><em>bigIntObj</em>.toLocaleString(</code><code>[locales [, options]])</code></pre>

<h3 id="参数">参数</h3>

<p><code>locales</code> 和 <code>options</code> 参数可自定义函数的行为,并允许应用程序指定应使用其格式约定的语言。在忽略 <code>locales</code> 和 <code>options</code> 参数的实现中,使用的 <code>locale</code> 和返回的字符串形式完全依赖于实现。</p>

<div>{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}</div>

<h3 id="返回值">返回值</h3>

<p>具有此 <code>BigInt</code> 的 language-sensitive 表示形式的字符串。</p>

<h2 id="例子">例子</h2>

<h3 id="Using_toLocaleString">Using <code>toLocaleString</code></h3>

<p>在不指定语言环境的基本用法中,将返回默认语言环境中带默认选项的格式化字符串。</p>

<pre class="brush: js">var bigint = 3500n;

bigint.toLocaleString();
// Displays "3,500" if in U.S. English locale
</pre>

<h3 id="Using_locales">Using <code>locales</code></h3>

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

<pre class="brush: js">var 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 <a href="https://en.wikipedia.org/wiki/Eastern_Arabic_numerals">Eastern Arabic</a> 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
</pre>

<h3 id="Using_options">Using <code>options</code></h3>

<p><code>toLocaleString</code> 提供的结果可以使用 <code>options</code> 参数进行自定义:</p>

<pre class="brush: js">var 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
</pre>

<h2 id="性能">性能</h2>

<p>格式化大量数字时,最好创建 {{jsxref("NumberFormat")}} 对象并使用其 {{jsxref("NumberFormat.format")}} 属性提供的函数。</p>

<h2 id="标准">标准</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td><a href="https://tc39.es/ecma402/#sup-bigint.prototype.tolocalestring">BigInt</a></td>
   <td>Stage 3</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器支持">浏览器支持</h2>

<p>{{Compat("javascript.builtins.BigInt.toLocaleString")}}</p>

<h2 id="请参阅">请参阅</h2>

<ul>
 <li>{{jsxref("BigInt.toString()")}}</li>
</ul>