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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
---
title: Intl.NumberFormat
slug: Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
tags:
- Intl
- 国际化
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
---
<div>{{JSRef}}</div>
<p><strong><code>Intl.NumberFormat是对语言敏感的格式化数字类的构造器类</code></strong></p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><code>new Intl.NumberFormat([<var>locales</var>[, <var>options</var>]])
Intl.NumberFormat.call(<var>this</var>[, <var>locales</var>[, <var>options</var>]])
</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>locales</code></dt>
<dd>
<p>可选。缩写语言代码(BCP 47 language tag,例如: cmn-Hans-CN)的字符串或者这些字符串组成的数组. 关于参数 locales 的一般形式和解释请参见<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation" title="国际化的构造函数和其他构造函数的几个语言敏感的方法(可见See also)一样,使用同样的模式来识别语言区域和确定使用哪一种语言格式:他们都接收 locales 和 options 参数,使用 options.localeMatcher 属性指定的一个算法来对比应用请求的和支持的语言区域,来确定使用哪一个语言区域。">Intl page</a>. 下面的这些 Unicode 扩展键也是被允许的:</p>
<p>译者注:下面扩展的使用方式是language[-scripts][-region]-u-nu-*,例如:zh-u-nu-hanidec(表示中文十进制数字) </p>
<dl>
<dt><code>nu</code></dt>
<dd>要使用的编号系统。可能的值有: <code>"arab"</code>, <code>"arabext"</code>, <code>"bali"</code>, <code>"beng"</code>, <code>"deva"</code>, <code>"fullwide"</code>, <code>"gujr"</code>, <code>"guru"</code>, <code>"hanidec"(中文十进制数字)</code>, <code>"khmr"</code>, <code>"knda"</code>, <code>"laoo"</code>, <code>"latn"</code>, <code>"limb"</code>, <code>"mlym"</code>, <code>"mong"</code>, <code>"mymr"</code>, <code>"orya"</code>, <code>"tamldec"</code>, <code>"telu"</code>, <code>"thai"</code>, <code>"tibt"</code>.</dd>
</dl>
</dd>
<dt><code>options</code></dt>
<dd>
<p>可选. 包含一些或所有的下面属性的类:</p>
<ul>
<li><code>“decimal”</code> 用于纯数字格式;</li>
<li><code>“currency”</code> 用于货币格式;</li>
<li><code>“percent”</code> 用于百分比格式;</li>
<li><code>“unit”</code> {{Experimental_inline}} 用于单位格式</li>
</ul>
<dl>
<dt><code>localeMatcher</code></dt>
<dd>使用的 local 的匹配算法. 可能的值有 <code>"lookup</code> 和 <code>"best fit"</code>; 默认值是 <code>"best fit"</code>. 有关此选项更多的信息, 请参见 {{jsxref("Global_Objects/Intl", "Intl page", "#Locale_negotiation", 1)}}.</dd>
<dt><code>style</code></dt>
<dd>要使用的格式样式,默认为 “decimal”。</dd>
<dt><code>numberingSystem</code> {{Experimental_inline}}</dt>
<dd>编号系统。可能的值包括:"<code>arab</code>","<code>arabext</code>"," <code>bali</code>","<code>beng</code>","<code>deva</code>","<code>fullwide</code>"," <code>gujr</code>","<code>guru</code>","<code>hanidec</code>","<code>khmr</code>"," <code>knda</code>","<code>laoo</code>", "<code>latn</code>","<code>limb</code>","<code>mlym</code>"," <code>mong</code>","<code>mymr</code>","<code>orya</code>","<code>tamldec</code>"," <code>telu</code>","<code>thai</code>","<code>tibt</code>"。</dd>
<dt><code>unit</code>{{Experimental_inline}}</dt>
<dd><code>unit</code> 格式中使用的单位,可能的值为在 <a href="http://unicode.org/reports/tr35/tr35-general.html#Unit_Elements" rel="nofollow">UTS #35, Part 2, Section 6</a> 定义的核心单元标识符。已从<a href="https://github.com/unicode-org/cldr/blob/master/common/validity/unit.xml">完整列表</a>中选择了一个<a href="https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier">单位子集</a>以用于ECMAScript。可以将成对的简单单位与 <code>“ -per-”</code> 连接以组成一个复合单位。没有默认值;如果 <code>style</code> 为 <code>“unit”</code>,必须提供<code>unit</code> 属性。</dd>
<dt><code>unitDisplay</code>{{Experimental_inline}}</dt>
<dd><code>unit</code> 格式化中使用的单位格式化样式,默认值为“ short”。<code>“long”</code> (e.g., <code>16 litres</code>)、<code>“short“</code> (e.g., <code>16 l</code>)、<code>”narrow“</code> (e.g., <code>16l</code>)</dd>
<dt><code>currency</code></dt>
<dd>在货币格式化中使用的货币符号. 可能的值是<em>ISO</em>的货币代码 (the ISO 4217 currency codes,) 例如 <code>"USD"</code> 表示美元, <code>"EUR"</code> 表示欧元, 或者 <code>"CNY"是人民币</code> — 更多请参考 <a href="http://www.currency-iso.org/en/home/tables/table-a1.html">Current currency & funds code list</a>。没有默认值,如果 <code>style</code> 是 <code>“currency”</code>,必须提 <code>currency</code> 属性.</dd>
<dt><code>currencyDisplay</code></dt>
<dd>如何在货币格式化中显示货币. 可能的值有 <code>"symbol"</code> 表示使用本地化的货币符号,例如 €, <code>"code"</code> 表示使用国际标准组织货币代码, <code>"name"</code> 表示使用本地化的货币名称,如 <code>"dollar"</code>; 默认值是 <code>"symbol"</code>.</dd>
<dt><code>useGrouping</code></dt>
<dd>是否使用分组分隔符,如千位分隔符或千/万/亿分隔符。可能的值是 true 和 false,默认值是 true。</dd>
</dl>
<p>下面的属性分为两组:<code>minimumintegerdigits</code>,<code>minimumfractiondigits</code>,<code>maximumfractiondigits</code> 作为一组,<code>minimumsignificantdigits</code> 和 <code>maximumsignificantdigits</code> 作为一组。如果定义了第二组中的任意一个属性,则忽略第一组的设置.</p>
<dl>
<dt><code>minimumIntegerDigits</code></dt>
<dd>使用的整数数字的最小数目.可能的值是从1到21,默认值是1.</dd>
<dt><code>minimumFractionDigits</code></dt>
<dd>使用的小数位数的最小数目.可能的值是从 0 到 20;默认为普通的数字和百分比格式为 0;默认为货币格式是由 <a href="http://www.currency-iso.org/en/home/tables/table-a1.html">ISO 4217 currency code list</a> 提供 (如果列表中没有提供则值为 2)。</dd>
<dt><code>maximumFractionDigits</code></dt>
<dd>使用的小数位数的最大数目。可能的值是从 0 到 20;纯数字格式的默认值是<code>minimumfractiondigits</code> 和 3 中大的那一个;货币格式默认值是<code>minimumfractiondigits</code> 和 <a href="http://www.currency-iso.org/en/home/tables/table-a1.html">ISO 4217 currency code list</a> 中大的那一个(如果列表中没有提供则值为2);百分比格式默认值是 <code>minimumfractiondigits</code> 和 0 中大的那一个。</dd>
<dt><code>minimumSignificantDigits</code></dt>
<dd>使用的有效数字的最小数目。可能的值是从1到21;默认值是1。</dd>
<dt><code>maximumSignificantDigits</code></dt>
<dd>使用的有效数字的最大数量。可能的值是从1到21;默认是 21.</dd>
<dt><code>notation</code>{{Experimental_inline}}</dt>
<dd>该号码应显示的格式,默认为 <code>“standard”</code>。
<ul>
<li>"<code>standard</code>" 纯数字格式;</li>
<li>"<code>scientific</code>" 返回格式化数字的大小顺序;</li>
<li>"<code>engineering</code>" 当被三除时返回十的指数</li>
<li>"<code>compact</code>" 代表指数的字符串,默认使用 <code>“short”</code> 格式
<ul>
<li>"<code>compactDisplay</code>" 仅在 <code>notation</code> 为 <code>“compact”</code> 时使用,采用 <code>“short”</code>(默认)或“<code>long</code>”</li>
</ul>
</li>
</ul>
</dd>
</dl>
</dd>
</dl>
<h2 id="描述">描述</h2>
<h3 id="属性">属性</h3>
<dl>
<dt>{{jsxref("NumberFormat.prototype", "Intl.NumberFormat.prototype")}}</dt>
<dd>允许将属性添加到所有对象上。</dd>
</dl>
<h3 id="方法">方法</h3>
<dl>
<dt>{{jsxref("NumberFormat.supportedLocalesOf", "Intl.NumberFormat.supportedLocalesOf()")}}</dt>
<dd>返回一个数组包含的那些被提供的可以被支持的locales,运行时的默认的 locale 不会出现在该数组中.</dd>
</dl>
<h2 id="NumberFormat_实例"><code>NumberFormat</code> 实例</h2>
<h3 id="属性_2">属性</h3>
<p><code>NumberFormat</code> 实例继承一下原型的属性:</p>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/prototype', 'Properties')}}</div>
<h3 id="方法_2">方法</h3>
<p><code>NumberFormat</code> 实例继承一下原型的方法:</p>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/prototype', 'Methods')}}</div>
<h2 id="例子">例子</h2>
<h3 id="基本用法">基本用法</h3>
<p>在不指定locale的基本用法中, 一个使用默认locale和默认options的字符串被返回.</p>
<pre class="brush: js">var number = 3500;
console.log(new Intl.NumberFormat().format(number));
// 如果在美国英语地区 → '3,500'
</pre>
<h3 id="使用locales">使用<code>locales</code></h3>
<p>这个例子显示了一些本地化的数字格式的一些变化。为了获得用于您的应用程序的用户界面的语言格式,请确保设定了语言(可能还有一些回退语言)参数:</p>
<pre class="brush: js">var number = 123456.789;
// 德语使用逗号作为小数点,使用.作为千位分隔符
console.log(new Intl.NumberFormat('de-DE').format(number));
// → 123.456,789
// 大多数阿拉伯语国家使用阿拉伯语数字
console.log(new Intl.NumberFormat('ar-EG').format(number));
// → ١٢٣٤٥٦٫٧٨٩
// India uses thousands/lakh/crore separators
console.log(new Intl.NumberFormat('en-IN').format(number));
// → 1,23,456.789
// 通过编号系统中的nu扩展键请求, 例如<code>中文十进制数字</code>
console.log(new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(number));
// → 一二三,四五六.七八九
//当请求的语言不被支持,例如巴里,包含一个回滚语言印尼,这时候就会使用印尼语
console.log(new Intl.NumberFormat(['ban', 'id']).format(number));
// → 123.456,789
</pre>
<h3 id="可用的选项">可用的选项</h3>
<p>可用的选项参数,返回的值可以被设置成想要的形式:</p>
<pre class="brush: js">var number = 123456.789;
// 请求一个货币格式
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// → 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// → ¥123,457
// 只显示三个有效数字
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
// → 1,23,000
</pre>
<h3 id="Using_notation">Using <code>notation</code></h3>
<pre><code>console.log(new Intl.NumberFormat('en-US', { notation: "scientific" }).format(987654321));
// → 9.877E8
console.log(new Intl.NumberFormat('pt-PT', { notation: "scientific" }).format(987654321));
// → 9,877E8
console.log(new Intl.NumberFormat('en-GB', { notation: "engineering" }).format(987654321));
// → 987.654E6
console.log(new Intl.NumberFormat('de', { notation: "engineering" }).format(987654321));
// → 987,654E6
console.log(new Intl.NumberFormat('zh-CN', { notation: "compact" }).format(987654321));
// → 9.9亿
console.log(new Intl.NumberFormat('fr', { notation: "compact" , compactDisplay: "long" }).format(987654321));
// → 988 millions
console.log(new Intl.NumberFormat('en-GB', { notation: "compact" , compactDisplay: "short" }).format(987654321));
// → 988M</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范版本</th>
<th scope="col">规范状态</th>
<th scope="col">注解</th>
</tr>
<tr>
<td>{{SpecName('ES Int 1.0', '#sec-11.1', 'Intl.NumberFormat')}}</td>
<td>{{Spec2('ES Int 1.0')}}</td>
<td>初始定义</td>
</tr>
<tr>
<td>{{SpecName('ES Int 2.0', '#sec-11.1', 'Intl.NumberFormat')}}</td>
<td>{{Spec2('ES Int 2.0')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES Int Draft', '#numberformat-objects', 'Intl.NumberFormat')}}</td>
<td>{{Spec2('ES Int Draft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>{{Compat("javascript.builtins.Intl.NumberFormat")}}</div>
<h2 id="参见">参见</h2>
<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}</div>
|