diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/intl/numberformat | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/intl/numberformat')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/format/index.html | 92 | ||||
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/index.html | 247 |
2 files changed, 339 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/format/index.html b/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/format/index.html new file mode 100644 index 0000000000..4e481052b1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/format/index.html @@ -0,0 +1,92 @@ +--- +title: Intl.NumberFormat.prototype.format +slug: Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format +translation_of: Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format +--- +<div>{{JSRef}}</div> + +<p> <strong><code>Intl.NumberFormat.prototype.format</code></strong> 属性返回一个根据{{jsxref("NumberFormat")}}对象的语言环境和格式化选项,来格式化一个数字的getter函数。</p> + +<div>{{EmbedInteractiveExample("pages/js/intl-numberformat-prototype-format.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><var>numberFormat</var>.format(<var>number</var>)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>number</code></dt> + <dd>要格式化的数值。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>该函数返回一个根据{{jsxref("NumberFormat")}}对象的语言环境和格式化选项,来<code>format</code> 一个数字的函数。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_format">使用 <code>format</code></h3> + +<p>使用 <code>format</code> 格式化一个单一的货币值, 以俄罗斯为例:</p> + +<pre class="brush: js">var options = { style: 'currency', currency: 'RUB' }; +var numberFormat = new Intl.NumberFormat('ru-RU', options); +console.log(numberFormat.format(654321.987)); +// → "654 321,99 руб." +</pre> + +<h3 id="使用_format_和_map">使用 <code>format</code> 和 <code>map</code></h3> + +<p>使用 <code>format</code> 返回的函数来格式化数组中的所有数字。注意,该函数绑定到所获得的{{jsxref("NumberFormat")}},因此它可以直接传递给{{jsxref("Array.prototype.map")}}。</p> + +<pre class="brush: js">var a = [123456.789, 987654.321, 456789.123]; +var numberFormat = new Intl.NumberFormat('es-ES'); +var formatted = a.map(numberFormat.format); +console.log(formatted.join('; ')); +// → "123.456,789; 987.654,321; 456.789,123" +</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.3.2', 'Intl.NumberFormat.prototype.format')}}</td> + <td>{{Spec2('ES Int 1.0')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ES Int 2.0', '#sec-11.3.2', 'Intl.NumberFormat.prototype.format')}}</td> + <td>{{Spec2('ES Int 2.0')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES Int Draft', '#sec-Intl.NumberFormat.prototype.format', 'Intl.NumberFormat.prototype.format')}}</td> + <td>{{Spec2('ES Int Draft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.Intl.NumberFormat.format")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("NumberFormat", "Intl.NumberFormat")}}</li> + <li>{{jsxref("Number.prototype.toLocaleString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/index.html b/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/index.html new file mode 100644 index 0000000000..51520c2b83 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/intl/numberformat/index.html @@ -0,0 +1,247 @@ +--- +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”。</dd> + <dd> + <ul> + <li><code>“long”</code> (e.g., <code>16 litres</code>)</li> + <li><code>“short“</code> (e.g., <code>16 l</code>)</li> + <li><code>”narrow“</code> (e.g., <code>16l</code>)</li> + </ul> + </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 <font face="consolas, Liberation Mono, courier, monospace">notation</font></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></div> + +<div id="compat-mobile">{{Compat("javascript.builtins.Intl.NumberFormat")}}</div> + +<h2 id="参见">参见</h2> + +<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}</div> |
