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/number | |
| 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/number')
25 files changed, 2785 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/epsilon/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/epsilon/index.html new file mode 100644 index 0000000000..fea6ee1bb8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/epsilon/index.html @@ -0,0 +1,114 @@ +--- +title: Number.EPSILON +slug: Web/JavaScript/Reference/Global_Objects/Number/EPSILON +translation_of: Web/JavaScript/Reference/Global_Objects/Number/EPSILON +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.EPSILON</code></strong> 属性表示 1 与{{jsxref("Number")}}可表示的大于 1 的最小的浮点数之间的差值。</p> + +<p>你不必创建一个 {{jsxref("Number")}} 对象来访问这个静态属性(直接使用 <code>Number.EPSILON</code>)。</p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>EPSILON</code> 属性的值接近于 <code>2.2204460492503130808472633361816E-16</code>,或者 <code>2<sup>-52。</sup></code></p> + +<h2 id="示例">示例</h2> + +<h3 id="测试是否相等">测试是否相等</h3> + +<pre class="brush: js">x = 0.2; +y = 0.3; +z = 0.1; +equal = (Math.abs(x - y + z) < Number.EPSILON); +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<pre><code>if (Number.EPSILON === undefined) { + Number.EPSILON = Math.pow(2, -52); +}</code></pre> + + + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.epsilon', 'Number.EPSILON')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>最初定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.epsilon', 'Number.EPSILON')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("25.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>9</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("25.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>9</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/index.html new file mode 100644 index 0000000000..9e20355b94 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/index.html @@ -0,0 +1,195 @@ +--- +title: Number +slug: Web/JavaScript/Reference/Global_Objects/Number +tags: + - JavaScript + - Number + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +<div>{{JSRef}}</div> + +<p> JavaScript 的 <strong><code>Number</code></strong> 对象是经过封装的能让你处理数字值的对象。<code>Number</code> 对象由 <code>Number()</code> 构造器创建。</p> + +<p>JavaScript的<code>Number</code>类型为<a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic">双精度IEEE 754 64位浮点</a>类型。</p> + +<p>最近出了stage3{{jsxref("BigInt")}} <font><font>任意精度数字类型,已经进入stage3规范</font></font></p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre>new Number(<em>value</em>); +var <em>a</em> = new Number('123'); // a === 123 is false +var <em>b</em> = Number('123'); // b === 123 is true +<em>a</em> instanceof Number; // is true +<em>b</em> instanceof Number; // is false</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>被创建对象的数字值。</dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>Number</code> 对象主要用于:</p> + +<ul> + <li>如果参数无法被转换为数字,则返回 {{jsxref("NaN")}}。</li> + <li>在非构造器上下文中 (如:没有 {{jsxref("Operators/new", "new")}} 操作符),<code>Number</code> 能被用来执行类型转换。</li> +</ul> + +<h2 id="Properties" name="Properties">属性</h2> + +<dl> + <dt>{{jsxref("Number.EPSILON")}}</dt> + <dd>两个可表示(representable)数之间的最小间隔。</dd> + <dt>{{jsxref("Number.MAX_SAFE_INTEGER")}}</dt> + <dd>JavaScript 中最大的安全整数 (<code>2<sup>53</sup> - 1</code>)。</dd> + <dt>{{jsxref("Number.MAX_VALUE")}}</dt> + <dd>能表示的最大正数。最小的负数是 <code>-MAX_VALUE</code>。</dd> + <dt>{{jsxref("Number.MIN_SAFE_INTEGER")}}</dt> + <dd>JavaScript 中最小的安全整数 (<code>-(2<sup>53</sup> - 1)</code>).</dd> + <dt>{{jsxref("Number.MIN_VALUE")}}</dt> + <dd>能表示的最小正数即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 <code>-MIN_VALUE</code>。</dd> + <dt>{{jsxref("Number.NaN")}}</dt> + <dd>特殊的“非数字”值。</dd> + <dt>{{jsxref("Number.NEGATIVE_INFINITY")}}</dt> + <dd>特殊的负无穷大值,在溢出时返回该值。</dd> + <dt>{{jsxref("Number.POSITIVE_INFINITY")}}</dt> + <dd>特殊的正无穷大值,在溢出时返回该值。</dd> + <dt>{{jsxref("Number.prototype")}}</dt> + <dd>Number 对象上允许的额外属性。</dd> +</dl> + +<h2 id="Methods" name="Methods">方法</h2> + +<dl> + <dt>{{jsxref("Number.isNaN()")}}</dt> + <dd>确定传递的值是否是 NaN。</dd> + <dt>{{jsxref("Number.isFinite()")}}</dt> + <dd>确定传递的值类型及本身是否是有限数。</dd> + <dt>{{jsxref("Number.isInteger()")}}</dt> + <dd>确定传递的值类型是“number”,且是整数。</dd> + <dt>{{jsxref("Number.isSafeInteger()")}}</dt> + <dd>确定传递的值是否为安全整数 ( -<code>(2<sup>53</sup> - 1)</code> 至 <code>2<sup>53</sup> - 1之间</code>)。</dd> + <dt>{{jsxref("Number.toInteger()")}} {{obsolete_inline}}</dt> + <dd>计算传递的值并将其转换为整数 (或无穷大)。</dd> +</dl> + +<div> +<dl> + <dt>{{jsxref("Number.parseFloat()")}}</dt> + <dd>和全局对象 {{jsxref("parseFloat", "parseFloat()")}} 一样。</dd> + <dt>{{jsxref("Number.parseInt()")}}</dt> + <dd>和全局对象 {{jsxref("parseInt", "parseInt()")}} 一样。</dd> +</dl> +</div> + +<h2 id="Number_instances" name="Number_instances"><code>Number</code> 实例</h2> + +<p>所有 <code>Number</code> 实例都继承自 {{jsxref("Number.prototype")}}。<code>被修改的 Number</code> 构造器的原型对象对全部 <code>Number</code> 实例都生效。</p> + +<h3 id="Methods_of_number_instance" name="Methods_of_number_instance">方法</h3> + +<div>{{page('/zh-CN/docs/JavaScript/Reference/Global_Objects/Number/prototype', 'Methods')}}</div> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example_Using_the_Number_object_to_assign_values_to_numeric_variables" name="Example:_Using_the_Number_object_to_assign_values_to_numeric_variables">使用 Number 对象给数字变量赋值</h3> + +<p>下例使用 <code>Number</code> 对象的属性给几个数字变量赋值:</p> + +<pre class="brush: js">var biggestNum = Number.MAX_VALUE; +var smallestNum = Number.MIN_VALUE; +var infiniteNum = Number.POSITIVE_INFINITY; +var negInfiniteNum = Number.NEGATIVE_INFINITY; +var notANum = Number.NaN; +</pre> + +<h3 id="整数类型的范围">整数类型的范围</h3> + +<p>JavaScript 能够准确表示的整数范围在<code>-2^53</code>到<code>2^53</code>之间(不含两个端点),超过这个范围,无法精确表示这个整数。 (详情请参阅 ECMAScript standard, chapter <em><a href="https://www.ecma-international.org/ecma-262/#sec-ecmascript-language-types-number-type">6.1.6 The Number Type</a></em>):</p> + +<pre class="brush: js">var biggestInt = Number.MAX_SAFE_INTEGER; +//9007199254740991 +var smallestInt = Number.MIN_SAFE_INTEGER; +//-9007199254740991</pre> + +<p>在解析序列化的JSON时,如果JSON解析器将它们强制转换为Number类型,那么超出此范围的整数值可能会被破坏。在工作中使用{{jsxref("String")}} 类型代替,是一个可行的解决方案。</p> + +<h3 id="Example_Using_Number_to_convert_a_Date_object" name="Example:_Using_Number_to_convert_a_Date_object">使用 <code>Number</code> 转换 <code>Date</code> 对象</h3> + +<p>下例使用 Number 作为函数来转换 <code>Date</code> 对象为数字值:</p> + +<pre class="brush: js">var d = new Date("December 17, 1995 03:24:00"); +print(Number(d)); +</pre> + +<p>这将输出 "819199440000"。</p> + +<h3 id="转换数字字符串为数字">转换数字字符串为数字</h3> + +<pre class="brush: js" dir="rtl">Number('123') // 123 +Number('12.3') // 12.3 +<code>Number('12.00') // 12</code> +Number('123e-1') // 12.3 +Number('') // 0 +Number(null) // 0 +Number('0x11') // 17 +Number('0b11') // 3 +Number('0o11') // 9 +Number('foo') // NaN +Number('100a') // NaN +Number('-Infinity') //-Infinity</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>初始定义。 实现于 JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7', 'Number')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number-objects', 'Number')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>新增了方法和属性: {{jsxref("Number.EPSILON", "EPSILON")}}, {{jsxref("Number.isFinite", "isFinite")}}, {{jsxref("Number.isInteger", "isInteger")}}, {{jsxref("Number.isNaN", "isNaN")}}, {{jsxref("Number.parseFloat", "parseFloat")}}, {{jsxref("Number.parseInt", "parseInt")}}</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number-objects', 'Number')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div id="compat-mobile"> +<div class="hidden"> +<p>The compatibility table in 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.</p> +</div> + +<p>{{Compat("javascript.builtins.Number")}}</p> +</div> + +<h2 id="See_also" name="See_also">参阅</h2> + +<ul> + <li>{{jsxref("NaN")}}</li> + <li>全局对象 {{jsxref("Math")}} </li> + <li>Integers with arbitrary precision: {{jsxref("BigInt")}}</li> + <li><a href="https://medium.com/@maximus.koretskyi/javascripts-number-type-8d59199db1b6#.9whwe88tz">Number type in details</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/isfinite/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/isfinite/index.html new file mode 100644 index 0000000000..aef163cfb6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/isfinite/index.html @@ -0,0 +1,89 @@ +--- +title: Number.isFinite() +slug: Web/JavaScript/Reference/Global_Objects/Number/isFinite +tags: + - Experimental + - JavaScript + - Method + - Number + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isFinite +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.isFinite()</code></strong> 方法用来检测传入的参数是否是一个有穷数。</p> + +<p>{{EmbedInteractiveExample("pages/js/number-isfinite.html")}}</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox notranslate"><code>Number.isFinite(v<var>alue</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要被检测有穷性的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{jsxref("Boolean", "布尔值")}} 表示给定的值是否是一个有穷数。</p> + +<h2 id="描述">描述</h2> + +<p>和全局的 {{jsxref("Global_Objects/isFinite", "isFinite()")}} 函数相比,这个方法不会强制将一个非数值的参数转换成数值,这就意味着,只有数值类型的值,且是有穷的(finite),才返回 <code>true</code>。</p> + +<h2 id="Polyfill">Polyfill</h2> + +<pre class="brush: js notranslate">if (Number.isFinite === undefined) Number.isFinite = function(value) { + return typeof value === 'number' && isFinite(value); +}</pre> + +<h2 id="示例">示例</h2> + +<pre class="brush: js notranslate">Number.isFinite(Infinity); // false +Number.isFinite(NaN); // false +Number.isFinite(-Infinity); // false + +Number.isFinite(0); // true +Number.isFinite(2e64); // true + +Number.isFinite('0'); // false, would've been true with + // global isFinite('0') +Number.isFinite(null); // false, would've been true with + // global isFinite(null) +</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('ES6', '#sec-number.isfinite', 'Number.isInteger')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.isfinite', 'Number.isInteger')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{Compat("javascript.builtins.Number.isFinite")}}</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>The {{jsxref("Number")}} object it belongs to.</li> + <li>The global function {{jsxref("isFinite")}}.</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/isinteger/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/isinteger/index.html new file mode 100644 index 0000000000..742abe51a6 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/isinteger/index.html @@ -0,0 +1,92 @@ +--- +title: Number.isInteger() +slug: Web/JavaScript/Reference/Global_Objects/Number/isInteger +tags: + - JavaScript + - Method + - Number + - Reference + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isInteger +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.isInteger()</code></strong> 方法用来判断给定的参数是否为整数。</p> + +<p>{{EmbedInteractiveExample("pages/js/number-isinteger.html")}}</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>Number.isInteger(v<var>alue</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要判断此参数是否为整数</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>判断给定值是否是整数的 {{jsxref("Boolean")}} 值。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>如果被检测的值是整数,则返回 <code>true</code>,否则返回 <code>false</code>。注意 {{jsxref("Global_Objects/NaN", "NaN")}} 和正负 {{jsxref("Global_Objects/Infinity", "Infinity")}} 不是整数。</p> + +<h2 id="Example" name="Example">示例</h2> + +<pre class="brush: js">Number.isInteger(0); // true +Number.isInteger(1); // true +Number.isInteger(-100000); // true + +Number.isInteger(0.1); // false +Number.isInteger(Math.PI); // false + +Number.isInteger(Infinity); // false +Number.isInteger(-Infinity); // false +Number.isInteger("10"); // false +Number.isInteger(true); // false +Number.isInteger(false); // false +Number.isInteger([1]); // false</pre> + +<h2 id="Polyfill" name="Polyfill">Polyfill</h2> + +<pre class="brush: js">Number.isInteger = Number.isInteger || function(value) { + return typeof value === "number" && + isFinite(value) && + Math.floor(value) === value; +}; +</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('ES6', '#sec-number.isinteger', 'Number.isInteger')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.isinteger', 'Number.isInteger')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Number.isInteger")}}</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Global_Objects/Number", "Number")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/isnan/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/isnan/index.html new file mode 100644 index 0000000000..19be268773 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/isnan/index.html @@ -0,0 +1,102 @@ +--- +title: Number.isNaN() +slug: Web/JavaScript/Reference/Global_Objects/Number/isNaN +tags: + - ECMAScript 2015 + - JavaScript + - Method + - NaN + - Number + - 数字 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isNaN +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.isNaN()</code></strong> 方法确定传递的值是否为 {{jsxref("NaN")}},并且检查其类型是否为 {{jsxref("Number")}}。它是原来的全局 {{jsxref("isNaN", "isNaN()")}} 的更稳妥的版本。</p> + +<p>{{EmbedInteractiveExample("pages/js/number-isnan.html", "taller")}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Number.isNaN(v<var>alue</var>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>要检测是否为 {{jsxref("Global_Objects/NaN", "NaN")}} 的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个{{jsxref("Boolean", "布尔值")}},表示给定的值是否是 {{jsxref("NaN")}}。</p> + +<h2 id="描述">描述</h2> + +<p>在 JavaScript 中,<code>NaN</code> 最特殊的地方就是,我们不能使用相等运算符({{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} 和 {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}})来判断一个值是否是 <code>NaN</code>,因为 <code>NaN == NaN</code> 和 <code>NaN === NaN</code> 都会返回 <code>false</code>。因此,必须要有一个判断值是否是 <code>NaN</code> 的方法。</p> + +<p>和全局函数 {{jsxref("Global_Objects/isNaN", "isNaN()")}} 相比,<code>Number.isNaN()</code> 不会自行将参数转换成数字,只有在参数是值为 <code>NaN</code> 的数字时,才会返回 <code>true</code>。</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">Number.isNaN(NaN); // true +Number.isNaN(Number.NaN); // true +Number.isNaN(0 / 0) // true + +// 下面这几个如果使用全局的 isNaN() 时,会返回 true。 +Number.isNaN("NaN"); // false,字符串 "NaN" 不会被隐式转换成数字 NaN。 +Number.isNaN(undefined); // false +Number.isNaN({}); // false +Number.isNaN("blabla"); // false + +// 下面的都返回 false +Number.isNaN(true); +Number.isNaN(null); +Number.isNaN(37); +Number.isNaN("37"); +Number.isNaN("37.37"); +Number.isNaN(""); +Number.isNaN(" ");</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<pre class="brush: js">Number.isNaN = Number.isNaN || function(value) { + return typeof value === "number" && isNaN(value); +} +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.isnan', 'Number.isnan')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<div>{{Compat("javascript.builtins.Number.isNaN")}}</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number")}}</li> + <li>{{jsxref("isNaN", "isNaN()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/issafeinteger/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/issafeinteger/index.html new file mode 100644 index 0000000000..e0755f390a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/issafeinteger/index.html @@ -0,0 +1,86 @@ +--- +title: Number.isSafeInteger() +slug: Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.isSafeInteger()</code></strong> 方法用来判断传入的参数值是否是一个“安全整数”(safe integer)。</p> + +<p>一个安全整数是一个符合下面条件的整数:</p> + +<ul> + <li>可以准确地表示为一个IEEE-754双精度数字,</li> + <li>其IEEE-754表示不能是舍入任何其他整数以适应IEEE-754表示的结果。.</li> +</ul> + +<p>比如,<code>2<sup>53</sup> - 1</code> 是一个安全整数,它能被精确表示,在任何 IEEE-754 舍入模式(rounding mode)下,没有其他整数舍入结果为该整数。作为对比,<code>2<sup>53</sup></code> 就不是一个安全整数,它能够使用 IEEE-754 表示,但是 <code>2<sup>53</sup> + 1</code> 不能使用 IEEE-754 直接表示,在就近舍入(round-to-nearest)和向零舍入中,会被舍入为 <code>2<sup>53</sup></code>。</p> + +<p>安全整数范围为 <code>-(2<sup>53</sup> - 1)到</code> <code>2<sup>53</sup> - 1 </code>之间的整数,包含 <code>-(2<sup>53</sup> - 1)和</code> <code>2<sup>53</sup> - 1</code>。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>Number.isSafeInteger(<var>testValue</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code><var>testValue</var></code></dt> + <dd>需要检测的参数。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个{{jsxref("Boolean", "布尔值")}} 表示给定的值是否是一个安全整数(safe integer)。</p> + +<h2 id="Example" name="Example">示例</h2> + +<pre class="brush:js">Number.isSafeInteger(3); // true +Number.isSafeInteger(Math.pow(2, 53)) // false +Number.isSafeInteger(Math.pow(2, 53) - 1) // true +Number.isSafeInteger(NaN); // false +Number.isSafeInteger(Infinity); // false +Number.isSafeInteger("3"); // false +Number.isSafeInteger(3.1); // false +Number.isSafeInteger(3.0); // true +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<pre><code>Number.isSafeInteger = Number.isSafeInteger || function (value) { + return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER; +};</code></pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.issafeinteger', 'Number.isSafeInteger')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.issafeinteger', 'Number.isSafeInteger')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Number.isSafeInteger")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li> {{jsxref("Number")}}</li> + <li>{{jsxref("Number.MIN_SAFE_INTEGER")}}</li> + <li>{{jsxref("Number.MAX_SAFE_INTEGER")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/max_safe_integer/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/max_safe_integer/index.html new file mode 100644 index 0000000000..2222e5a5ff --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/max_safe_integer/index.html @@ -0,0 +1,105 @@ +--- +title: Number.MAX_SAFE_INTEGER +slug: Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER +translation_of: Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.MAX_SAFE_INTEGER</code></strong> 常量表示在 JavaScript 中最大的安全整数(maxinum safe integer)(<code>2<sup>53</sup> - 1)。</code></p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>MAX_SAFE_INTEGER 是一个值为 9007199254740991的常量。因为Javascript的数字存储使用了</code><a href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a>中规定的<a href="https://zh.wikipedia.org/zh-cn/%E9%9B%99%E7%B2%BE%E5%BA%A6%E6%B5%AE%E9%BB%9E%E6%95%B8">双精度浮点数</a>数据类型,而这一数据类型能够安全存储 <code>-(2<sup>53</sup> - 1)</code> 到 <code>2<sup>53</sup> - 1 之间的数值(包含边界值)。</code></p> + +<p>这里安全存储的意思是指能够准确区分两个不相同的值,例如 <code>Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 将得到 true的结果,而这在数学上是错误的,参考</code>{{jsxref("Number.isSafeInteger()")}}获取更多信息.</p> + +<p>由于 <code>MAX_SAFE_INTEGER 是</code>{{jsxref("Number")}}的一个<code>静态属性,所以你不用自己动手为</code>{{jsxref("Number")}}对象创建<code>Number.MAX_SAFE_INTEGER</code>这一属性,就可以直接使用它。</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">Number.MAX_SAFE_INTEGER // 9007199254740991 +Math.pow(2, 53) - 1 // 9007199254740991 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.max_safe_integer', 'Number.MAX_SAFE_INTEGER')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>首次定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.max_safe_integer', 'Number.MAX_SAFE_INTEGER')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome("34")}}</td> + <td>{{CompatGeckoDesktop("31")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>9.0.2</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("32")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number.MIN_SAFE_INTEGER")}}</li> + <li>{{jsxref("Number.isSafeInteger()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/max_value/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/max_value/index.html new file mode 100644 index 0000000000..974f0a46e0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/max_value/index.html @@ -0,0 +1,115 @@ +--- +title: Number.MAX_VALUE +slug: Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE +translation_of: Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><code><strong>Number.MAX_VALUE</strong></code> 属性表示在 JavaScript 里所能表示的最大数值。</p> + +<p>{{js_property_attributes(0,0,0)}}</p> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>MAX_VALUE</code> 属性值接近于 <code>1.79E+308</code>。大于 <code>MAX_VALUE</code> 的值代表 "<code>Infinity</code>"。</p> + +<p>因为 <code>MAX_VALUE</code> 是 <code>Number</code> 对象的一个静态属性,所以你应该直接使用<code>Number.MAX_VALUE</code> ,而不是作为一个创建的 <code>Number</code> 实例的属性。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example:_Using_MAX_VALUE" name="Example:_Using_MAX_VALUE">例子:使用 <code>MAX_VALUE</code></h3> + +<p>下面的代码求两个数值的乘积。如果结果小于等于 <code>MAX_VALUE</code>,则调用 <code>func1</code> 函数;否则,调用 <code>func2</code> 函数。</p> + +<pre class="brush: js">if (num1 * num2 <= Number.MAX_VALUE) { + func1(); +} else { + func2(); +}</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>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.3.2', 'Number.MAX_VALUE')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.max_value', 'Number.MAX_VALUE')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Number.MIN_VALUE")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/min_safe_integer/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/min_safe_integer/index.html new file mode 100644 index 0000000000..9604955e5b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/min_safe_integer/index.html @@ -0,0 +1,58 @@ +--- +title: Number.MIN_SAFE_INTEGER +slug: Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER +translation_of: Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number.MIN_SAFE_INTEGER</code></strong> 代表在 JavaScript中最小的安全的integer型数字 (<code>-(2<sup>53</sup> - 1)</code>).</p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="描述">描述</h2> + +<p><code>MIN_SAFE_INTEGER</code> 的值是<code>-9007199254740991</code>. 形成这个数字的原因是 JavaScript 在 <a href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a>中使用<a href="http://en.wikipedia.org/wiki/Double_precision_floating-point_format">double-precision floating-point format numbers</a> 作为规定。在这个规定中能安全的表示数字的范围在<code>-(2<sup>53</sup> - 1)</code> 到 <code>2<sup>53</sup> - 1之间</code>.</p> + +<p><code>由于MIN_SAFE_INTEGER</code> 是{{jsxref("Number")}}的一个静态属性,你可以直接使用<code>Number.MIN_SAFE_INTEGER</code>, 而不是自己去创建一个{{jsxref("Number")}}的属性。 </p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">Number.MIN_SAFE_INTEGER // -9007199254740991 +-(Math.pow(2, 53) - 1) // -9007199254740991 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.min_safe_integer', 'Number.MIN_SAFE_INTEGER')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.min_safe_integer', 'Number.MIN_SAFE_INTEGER')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器支持">浏览器支持</h2> + +<div>{{CompatibilityTable}}</div> + +<p>{{Compat("javascript.builtins.Number.MIN_SAFE_INTEGER")}}</p> + + +<h2 id="参阅">参阅</h2> + +<ul> + <li>{{jsxref("Number.MAX_SAFE_INTEGER")}}</li> + <li>{{jsxref("Number.isSafeInteger()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/min_value/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/min_value/index.html new file mode 100644 index 0000000000..0b90f3eb28 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/min_value/index.html @@ -0,0 +1,99 @@ +--- +title: Number.MIN_VALUE +slug: Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE +translation_of: Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE +--- +<div> + {{JSRef("Global_Objects", "Number")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>Number.MIN_VALUE</strong></code> 属性表示在 JavaScript 中所能表示的最小的正值。</p> +<p>{{js_property_attributes(0,0,0)}}</p> +<h2 id="Description" name="Description">描述</h2> +<p><code>MIN_VALUE</code> 属性是 JavaScript 里最接近 0 的正值,而不是最小的负值。</p> +<p><code>MIN_VALUE</code> 的值约为 5e-324。小于 <code>MIN_VALUE</code> ("underflow values") 的值将会转换为 0。</p> +<p>因为 <code>MIN_VALUE</code> 是 <code>Number</code> 的一个静态属性,因此应该直接使用: <code>Number.MIN_VALUE,</code> 而不是作为一个创建的 <code>Number</code> 实例的属性。</p> +<h2 id="Examples" name="Examples">示例</h2> +<h3 id="Example:_Using_MIN_VALUE" name="Example:_Using_MIN_VALUE">例子:使用 <code>MIN_VALUE</code></h3> +<p>下面的代码里两个数值相除。如果结果大于或等于 <code>MIN_VALUE</code>,则调用 <code>func1</code> 函数;否则,调用 <code>func2</code> 函数。</p> +<pre class="brush:js">if (num1 / num2 >= Number.MIN_VALUE) { + func1(); +} else { + func2(); +}</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>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.3.3', 'Number.MIN_VALUE')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.min_value', 'Number.MIN_VALUE')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("Number.MAX_VALUE")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/nan/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/nan/index.html new file mode 100644 index 0000000000..9921175594 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/nan/index.html @@ -0,0 +1,105 @@ +--- +title: Number.NaN +slug: Web/JavaScript/Reference/Global_Objects/Number/NaN +translation_of: Web/JavaScript/Reference/Global_Objects/Number/NaN +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary" style="margin-bottom: 20px; line-height: 30px;">概述</h2> + +<p><code><strong>Number.NaN</strong></code> 表示“非数字”(Not-A-Number)。和 {{jsxref("Global_Objects/NaN", "NaN")}} 相同。</p> + +<p>不必创建一个 {{jsxref("Global_Objects/Number", "Number")}} 实例来访问该属性,使用 <code>Number.NaN</code> 来访问该静态属性。</p> + +<p>{{js_property_attributes(0,0,0)}}</p> + +<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.3.4', 'Number.NaN')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.nan', 'Number.NaN')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.nan', 'Number.NaN')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Chrome</th> + <th style="line-height: 16px;">Firefox (Gecko)</th> + <th style="line-height: 16px;">Internet Explorer</th> + <th style="line-height: 16px;">Opera</th> + <th style="line-height: 16px;">Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th style="line-height: 16px;">Feature</th> + <th style="line-height: 16px;">Android</th> + <th style="line-height: 16px;">Chrome for Android</th> + <th style="line-height: 16px;">Firefox Mobile (Gecko)</th> + <th style="line-height: 16px;">IE Mobile</th> + <th style="line-height: 16px;">Opera Mobile</th> + <th style="line-height: 16px;">Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2> + +<ul> + <li>全局对象 {{jsxref("Global_Objects/NaN", "NaN")}}</li> + <li>{{jsxref("Global_Objects/Number", "Number")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/negative_infinity/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/negative_infinity/index.html new file mode 100644 index 0000000000..6649246cf5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/negative_infinity/index.html @@ -0,0 +1,112 @@ +--- +title: Number.NEGATIVE_INFINITY +slug: Web/JavaScript/Reference/Global_Objects/Number/NEGATIVE_INFINITY +translation_of: Web/JavaScript/Reference/Global_Objects/Number/NEGATIVE_INFINITY +--- +<div> + {{JSRef("Global_Objects", "Number")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>Number.NEGATIVE_INFINITY</strong></code> 属性表示负无穷大。</p> +<p>不用创建一个 {{jsxref("Global_Objects/Number", "Number")}} 实例,使用 <code>Number.NEGATIVE_INFINITY</code> 来访问该静态属性。</p> +<p>{{js_property_attributes(0,0,0)}}</p> +<h2 id="Description" name="Description">描述</h2> +<p><code>Number.NEGATIVE_INFINITY</code> 的值和全局对象的 {{jsxref("Global_Objects/Infinity", "Infinity")}} 属性的负值相同。</p> +<p>该值的行为同数学上的无穷大(infinity)有一点儿不同:</p> +<ul> + <li>任何正值,包括 <code>POSITIVE_INFINITY,</code>乘以 <code>NEGATIVE_INFINITY</code> 为 <code>NEGATIVE_INFINITY</code>。</li> + <li>任何负值,包括 <code>NEGATIVE_INFINITY</code>,乘以 <code>NEGATIVE_INFINITY</code> 为 <code>POSITIVE_INFINITY</code>。</li> + <li>0 乘以 <code>NEGATIVE_INFINITY</code> 为 <code>NaN</code>.</li> + <li>NaN 乘以 <code>NEGATIVE_INFINITY</code> 为<code> NaN</code>.</li> + <li><code>NEGATIVE_INFINITY</code> 除以任何负值(除了 <code>NEGATIVE_INFINITY)</code>为 <code>POSITIVE_INFINITY</code>。</li> + <li><code>NEGATIVE_INFINITY</code> 除以任何正值(除了 <code>POSITIVE_INFINITY</code>)为 <code>NEGATIVE_INFINITY</code>。</li> + <li><code>NEGATIVE_INFINITY</code> 除以 <code>NEGATIVE_INFINITY</code> 或 <code>POSITIVE_INFINITY</code> 是 <code>NaN</code>。</li> + <li>任何数除以 <code>NEGATIVE_INFINITY</code> 为 0。</li> +</ul> +<p>为了成功返回一个有限值,你可能会使用 <code>Number.NEGATIVE_INFINITY</code> 属性来判断是否显示一个条件错误 。然而 {{jsxref("Global_Objects/isFinite", "isFinite")}} 方法更适合这种情况。</p> +<h2 id="Example" name="Example">示例</h2> +<p>下例中,赋值给变量 <code>smallNumber</code> 一个明显小于 JavaScript 中的最小值的值。当 <code>if</code> 语句执行时,<code>smallNumber</code> 值为 "<code>-Infinity</code>",因此在继续执行代码前,<code>smallNumber</code> 被设为一个更容易管理的值。</p> +<pre class="brush:js">var smallNumber = (-Number.MAX_VALUE) * 2 + +if (smallNumber == Number.NEGATIVE_INFINITY) { + smallNumber = returnFinite(); +} +</pre> +<h2 id="规范">规范</h2> +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.3.5', 'Number.NEGATIVE_INFINITY')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.negative_infinity', 'Number.NEGATIVE_INFINITY')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器规范">浏览器规范</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_also" name="See_also">相关链接</h2> +<ul> + <li>{{jsxref("Number.POSITIVE_INFINITY")}}</li> + <li>{{jsxref("Global_Objects/Infinity", "Infinity")}}</li> + <li>{{jsxref("Global_Objects/isFinite", "isFinite")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/number/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/number/index.html new file mode 100644 index 0000000000..e539061589 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/number/index.html @@ -0,0 +1,58 @@ +--- +title: Number() constructor +slug: Web/JavaScript/Reference/Global_Objects/Number/Number +translation_of: Web/JavaScript/Reference/Global_Objects/Number/Number +--- +<div>{{JSRef}}</div> + +<p><strong><code>Number()</code> 构造函数创建一个</strong> {{jsxref("Number")}} 对象。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js notranslate">new Number(<var>value</var>) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><var>value</var></code></dt> + <dd>创建的 <code>Number</code> 对象的数值。</dd> +</dl> + +<h2 id="例子">例子</h2> + +<h3 id="创建_Number_对象">创建 Number 对象</h3> + +<pre class="brush: js notranslate">const a = new Number('123'); // a === 123 is false +const b = Number('123'); // b === 123 is true +a instanceof Number; // is true +b instanceof Number; // is false</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-number-constructor', 'Number constructor')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Number.Number")}}</p> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li>{{jsxref("NaN")}}</li> + <li>全局对象 {{jsxref("Math")}}</li> + <li>任意精度的整数 {{jsxref("BigInt")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/parsefloat/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/parsefloat/index.html new file mode 100644 index 0000000000..f55a3ceba8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/parsefloat/index.html @@ -0,0 +1,109 @@ +--- +title: Number.parseFloat() +slug: Web/JavaScript/Reference/Global_Objects/Number/parseFloat +translation_of: Web/JavaScript/Reference/Global_Objects/Number/parseFloat +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong><code>Number.parseFloat()</code></strong> 方法可以把一个字符串解析成浮点数。该方法与全局的 {{jsxref("Global_Objects/parseFloat", "parseFloat()")}} 函数相同,并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>Number.parseFloat(<var>string</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>string</code></dt> + <dd>被解析的字符串。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dd>给定值被解析成浮点数,如果无法被解析成浮点数,则返回<code>NaN</code></dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>请移步全局函数 {{jsxref("Global_Objects/parseFloat", "parseFloat()")}} 页面查看更多的解释和示例。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范版本</th> + <th scope="col">规范状态</th> + <th scope="col">注解</th> + </tr> + <tr> + <td> + <p>{{SpecName('ES6', '#sec-number.parsefloat', 'Number.parseFloat')}}</p> + </td> + <td>{{Spec2('ES6')}}</td> + <td>首次定义</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("25")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("25")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Global_Objects/Number", "Number")}}</li> + <li>{{jsxref("Global_Objects/parseFloat", "parseFloat()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/parseint/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/parseint/index.html new file mode 100644 index 0000000000..56f1d834be --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/parseint/index.html @@ -0,0 +1,82 @@ +--- +title: Number.parseInt() +slug: Web/JavaScript/Reference/Global_Objects/Number/parseInt +tags: + - ECMAScript 2015 + - JavaScript + - Method + - Number +translation_of: Web/JavaScript/Reference/Global_Objects/Number/parseInt +--- +<div>{{JSRef}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong><code>Number.parseInt()</code></strong> 方法依据指定基数 [ 参数 <strong>radix </strong>的值],把字符串 [ 参数 <strong>string</strong> 的值] 解析成整数。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>Number.parseInt(<var>string</var>[, <var>radix</var>])</code> +</pre> + +<h3 id="参数">参数</h3> + +<p>{{page("en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt", "Parameters")}} </p> + +<p>参数string:要解析的值。 如果此参数不是字符串,则使用ToString抽象操作将其转换为字符串。忽略此参数中的前导空格。<br> + 参数radix:一个介于2到36之间的整数,代表字符串的基数(数学数字系统中的基)。小心-这并不是默认为10。</p> + +<h3 id="返回值">返回值</h3> + +<p>{{page("en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt", "Return value")}}</p> + +<p>从给定字符串中解析的整数。如果基数小于11,且第一个非空白字符不能转换为数字,则返回NaN。</p> + +<h2 id="描述">描述</h2> + +<p>这个方法和全局的 {{jsxref("parseInt", "parseInt()")}} 函数具有一样的函数功能:</p> + +<pre><code>Number.parseInt === parseInt; // true</code></pre> + +<p>ECMAScript 2015添加了这部分 (其目的是对全局变量进行模块化). 请另见 {{jsxref("parseInt", "parseInt()")}} 获取更多详情和示例.</p> + +<h2 id="Polyfill">Polyfill</h2> + +<pre><code>if (Number.parseInt === undefined) { + Number.parseInt = window.parseInt; +}</code></pre> + +<h2 id="说明">说明</h2> + +<table> + <tbody> + <tr> + <th scope="col">说明</th> + <th scope="col">状态</th> + <th scope="col">评论</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-number.parseint', 'Number.parseInt')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>初始定义.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-number.parseint', 'Number.parseInt')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>The compatibility table in 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.</p> + +<p>{{Compat("javascript.builtins.Number.parseInt")}}</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li><code>{{jsxref("Global_Objects/Number", "Number()")}}</code></li> + <li>全局的 {{jsxref("Global_Objects/parseInt", "parseInt()")}} 函数</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/positive_infinity/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/positive_infinity/index.html new file mode 100644 index 0000000000..4207e72e5a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/positive_infinity/index.html @@ -0,0 +1,130 @@ +--- +title: Number.POSITIVE_INFINITY +slug: Web/JavaScript/Reference/Global_Objects/Number/POSITIVE_INFINITY +translation_of: Web/JavaScript/Reference/Global_Objects/Number/POSITIVE_INFINITY +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="概述">概述</h2> + +<p><code><strong>Number.POSITIVE_INFINITY</strong></code> 属性表示正无穷大。</p> + +<p>不必创建一个 {{jsxref("Global_Objects/Number", "Number")}} 实例,可使用 <code>Number.POSITIVE_INFINITY</code> 来访问该静态属性。</p> + +<p>{{js_property_attributes(0,0,0)}}</p> + +<h2 id="描述">描述</h2> + +<p><code>Number.POSITIVE_INFINITY</code> 的值同全局对象 {{jsxref("Global_Objects/Infinity", "Infinity")}} 属性的值相同。</p> + +<p>该值的表现同数学上的无穷大有点儿不同:</p> + +<ul> + <li>任何正值,包括 <code>POSITIVE_INFINITY</code>,乘以 <code>POSITIVE_INFINITY</code> 为 <code>POSITIVE_INFINITY</code>。</li> + <li>任何负值,包括 <code>NEGATIVE_INFINITY</code>,乘以 <code>POSITIVE_INFINITY</code> 为 <code>NEGATIVE_INFINITY</code>。</li> + <li>0 乘以 <code>POSITIVE_INFINITY</code> 为 NaN。</li> + <li>NaN 乘以 <code>POSITIVE_INFINITY</code> 为 NaN。</li> + <li><code>POSITIVE_INFINITY</code> 除以 <code>NEGATIVE_INFINITY</code> 以外的任何负值为 <code>NEGATIVE_INFINITY</code>。</li> + <li><code>POSITIVE_INFINITY</code> 除以 <code>POSITIVE_INFINITY</code> 以外的任何正值为 <code>POSITIVE_INFINITY</code>。</li> + <li><code>POSITIVE_INFINITY</code> 除以 <code>NEGATIVE_INFINITY</code> 或 <code>POSITIVE_INFINITY</code> 为 NaN。</li> + <li>任何数除以 <code>POSITIVE_INFINITY</code> 为 0。</li> +</ul> + +<p>You might use the <code>Number.POSITIVE_INFINITY</code> property to indicate an error condition that returns a finite number in case of success. Note, however, that {{jsxref("Global_Objects/isFinite", "isFinite")}} would be more appropriate in such a case.</p> + +<h2 id="示例">示例</h2> + +<p>下例中,赋值给变量 <code>bigNumber</code> 一个大于 JavaScript 中最大值的值。当 <code>if</code> 语句执行时,变量 <code>bigNumber</code> 值为 "<code>Infinity</code>", 因此在继续执行代码前,为变量 <code>bigNumber</code> 设置一个容易管理的值。</p> + +<pre class="brush: js">var bigNumber = Number.MAX_VALUE * 2 +if (bigNumber == Number.POSITIVE_INFINITY) { + bigNumber = returnFinite(); +} +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.3.6', 'Number.POSITIVE_INFINITY')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.positive_infinity', 'Number.POSITIVE_INFINITY')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Number.NEGATIVE_INFINITY")}}</li> + <li>{{jsxref("Global_Objects/Infinity", "Infinity")}}</li> + <li>{{jsxref("Global_Objects/isFinite", "isFinite")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html new file mode 100644 index 0000000000..578011f564 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/prototype/index.html @@ -0,0 +1,131 @@ +--- +title: Number.prototype +slug: Web/JavaScript/Reference/Global_Objects/Number/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +<div> + {{JSRef("Global_Objects", "Number")}}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><strong><code>Number.prototype</code></strong> 属性表示 {{jsxref("Global_Objects/Number", "Number")}} 构造函数的原型。</p> +<div> + {{js_property_attributes(0,0,0)}}</div> +<h2 id="Description" name="Description">描述</h2> +<p>所有 <code>Number</code> 实例都继承自 <code>Number.prototype</code>。修改 {{jsxref("Global_Objects/Number", "Number")}} 构造函数的原型对象会影响到所有 <code>Number</code> 实例。.</p> +<h2 id="属性">属性</h2> +<dl> + <dt> + constructor</dt> + <dd> + 返回创建该实例对象的构造函数。默认为 {{jsxref("Global_Objects/Number", "Number")}} 对象。</dd> +</dl> +<div> + {{ jsOverrides("Object", "properties", "constructor") }}</div> +<h2 id="方法">方法</h2> +<dl> + <dt> + {{jsxref("Number.prototype.toExponential()")}}</dt> + <dd> + 返回一个使用指数表示法表示的该数值的字符串表示。</dd> + <dt> + {{jsxref("Number.prototype.toFixed()")}}</dt> + <dd> + 返回一个使用定点表示法表示的该数值的字符串表示。</dd> + <dt> + {{jsxref("Number.prototype.toLocaleString()")}}</dt> + <dd> + 返回一个与语言相关的该数值对象的字符串表示。覆盖了{{jsxref("Object.prototype.toLocaleString()")}} 方法。</dd> + <dt> + {{jsxref("Number.prototype.toPrecision()")}}</dt> + <dd> + 使用定点表示法或指数表示法来表示的指定显示位数的该数值对象的字符串表示。</dd> + <dt> + {{jsxref("Number.prototype.toSource()")}} {{ Non-standard_inline() }}</dt> + <dd> + Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.</dd> + <dt> + {{jsxref("Number.prototype.toString()")}}</dt> + <dd> + 返回一个表示该数值对象的字符串。覆盖了 {{jsxref("Object.prototype.toString()")}} 方法。</dd> + <dt> + {{jsxref("Number.prototype.valueOf()")}}</dt> + <dd> + 返回该数值对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。</dd> +</dl> +<div> + {{ jsOverrides("Object", "methods", "toExponential", "toFixed", "toLocaleString", "toPrecision", "toSource", "toString", "valueOf") }}</div> +<div> + </div> +<h2 id="规范">规范</h2> +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范版本</th> + <th scope="col">规范状态</th> + <th scope="col">注解</th> + </tr> + <tr> + <td>ECMAScript 1st Edition. Implemented in JavaScript 1.1</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4', 'Number')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-properties-of-the-number-prototype-object', 'Number')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<p> </p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/toexponential/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/toexponential/index.html new file mode 100644 index 0000000000..a19013e4c4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/toexponential/index.html @@ -0,0 +1,142 @@ +--- +title: Number.prototype.toExponential() +slug: Web/JavaScript/Reference/Global_Objects/Number/toExponential +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toExponential +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong><code>toExponential()</code></strong> 方法以指数表示法返回该数值字符串表示形式。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>numObj</em>.toExponential(<em>fractionDigits</em>)</code></pre> + +<h3 id="Parameter" name="Parameter">参数</h3> + +<dl> + <dt>fractionDigits</dt> + <dd>可选。一个整数,用来指定小数点后有几位数字。默认情况下用尽可能多的位数来显示数字。</dd> +</dl> + +<h3 id="Returns" name="Returns">返回值</h3> + +<p>一个用幂的形式 (科学记数法) 来表示{{jsxref("Number")}} 对象的字符串。小数点后以fractionDigits 提供的值来四舍五入。如果 <code>fractionDigits</code> 参数被忽略了,小数点后的将尽可能用最多的位数来表示该数值。</p> + +<p>对数值字面量使用 <code>toExponential()</code> 方法,且该数值没有小数点和指数时,应该在该数值与该方法之间隔开一个空格,以避免点号被解释为一个小数点。也可以使用两个点号调用该方法。</p> + +<p>如果一个数值的小数位数多余 <code>fractionDigits</code> 参数所提供的,则该数值将会在 <code>fractionDigits</code> 指定的小数位数处四舍五入。可以查看 {{jsxref("Number.toFixed", "toFixed()")}} 方法描述中关于四舍五入的讨论,同样应用于 <code>toExponential() </code>方法。</p> + +<h3 id="Throws" name="Throws">异常</h3> + +<dl> + <dt>{{jsxref("Global_Objects/RangeError", "RangeError")}}</dt> + <dd>如果 <em>fractionDigits</em> 太小或太大将会抛出该错误。介于 0 和 20(包括20)之间的值不会引起 <code>RangeError</code> 。 执行环境也可以支持更大或更小范围。</dd> +</dl> + +<dl> + <dt>{{jsxref("Global_Objects/TypeError", "TypeError")}}</dt> + <dd>如果该方法在一个非数值类型对象上调用。</dd> +</dl> + +<h2 id="Example" name="Example">示例</h2> + +<pre class="brush:js">var numObj = 77.1234; + +alert("numObj.toExponential() is " + numObj.toExponential()); //输出 7.71234e+1 + +alert("numObj.toExponential(4) is " + numObj.toExponential(4)); //输出 7.7123e+1 + +alert("numObj.toExponential(2) is " + numObj.toExponential(2)); //输出 7.71e+1 + +alert("77.1234.toExponential() is " + 77.1234.toExponential()); //输出 7.71234e+1 + +alert("77 .toExponential() is " + 77 .toExponential()); //输出 7.7e+1</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.5</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.6', 'Number.prototype.toExponential')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.toexponential', 'Number.prototype.toExponential')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_Also" name="See_Also">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toFixed()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tofixed/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tofixed/index.html new file mode 100644 index 0000000000..03864f8c9e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/tofixed/index.html @@ -0,0 +1,115 @@ +--- +title: Number.prototype.toFixed() +slug: Web/JavaScript/Reference/Global_Objects/Number/toFixed +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed +--- +<div>{{JSRef}}</div> + +<p><code><strong>toFixed()</strong></code> 方法使用定点表示法来格式化一个数值。</p> + +<div>{{EmbedInteractiveExample("pages/js/number-tofixed.html")}}</div> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>numObj</em>.toFixed(<em>digits</em>)</code></pre> + +<h3 id="Parameter" name="Parameter">参数</h3> + +<dl> + <dt>digits</dt> + <dd>小数点后数字的个数;介于 0 到 20 (包括)之间,实现环境可能支持更大范围。如果忽略该参数,则默认为 0。</dd> +</dl> + +<h3 id="Returns" name="Returns">返回值</h3> + +<p>使用定点表示法表示给定数字的字符串。</p> + +<h3 id="Throws" name="Throws">抛出异常</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd>如果 <em>digits</em> 参数太小或太大。0 到 20(包括)之间的值不会引起 {{jsxref("RangeError")}}。实现环境(implementations)也可以支持更大或更小的值。</dd> +</dl> + +<dl> + <dt>{{jsxref("TypeError")}}</dt> + <dd>如果该方法在一个非{{jsxref( "Number")}}类型的对象上调用。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p><font face="Open Sans, sans-serif">一个数值的字符串表现形式,不使用指数记数法,而是在小数点后有 </font>digits<font face="Open Sans, sans-serif">(注:digits具体值取决于传入参数)位数字。</font>该数值在必要时进行四舍五入,另外在必要时会用 0 来填充小数部分,以便小数部分有指定的位数。 如果数值大于 1e+21,该方法会简单调用 {{jsxref("Number.prototype.toString()")}}并返回一个指数记数法格式的字符串。</p> + +<div class="warning"> +<p><strong>Warning:</strong> 浮点数不能精确地用二进制表示所有小数。这可能会导致意外的结果,例如 <code>0.1 + 0.2 === 0.3</code> 返回 <code>false</code> .</p> +</div> + +<h2 id="Example" name="Example">示例</h2> + +<h3 id="使用_toFixed">使用 <code>toFixed</code></h3> + +<pre class="brush: js">var numObj = 12345.6789; + +numObj.toFixed(); // 返回 "12346":进行四舍六入五看情况,不包括小数部分 +numObj.toFixed(1); // 返回 "12345.7":进行四舍六入五看情况 + +numObj.toFixed(6); // 返回 "12345.678900":用0填充 + +(1.23e+20).toFixed(2); // 返回 "123000000000000000000.00" + +(1.23e-10).toFixed(2); // 返回 "0.00" + +2.34.toFixed(1); // 返回 "2.3" + +2.35.toFixed(1) // 返回 '2.4'. Note it rounds up + +2.55.toFixed(1) // 返回 '2.5'. Note it rounds down - see warning above + +-2.34.toFixed(1); // 返回 -2.3 (由于操作符优先级,负数不会返回字符串) + +(-2.34).toFixed(1); // 返回 "-2.3" (若用括号提高优先级,则返回字符串) + +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition. Implemented in JavaScript 1.5.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.5', 'Number.prototype.toFixed')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Number.toFixed")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> + +<div id="gtx-trans" style="position: absolute; left: 561px; top: 1828px;"> +<div class="gtx-trans-icon"></div> +</div> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tointeger/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tointeger/index.html new file mode 100644 index 0000000000..24abe2125e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/tointeger/index.html @@ -0,0 +1,97 @@ +--- +title: Number.toInteger() +slug: Web/JavaScript/Reference/Global_Objects/Number/toInteger +translation_of: Archive/Web/JavaScript/Number.toInteger +--- +<div>{{JSRef("Global_Objects", "Number")}} {{obsolete_header("33")}} {{non-standard_header}}</div> + +<h2 id="Summary" name="Summary">概览</h2> + +<p><strong><code>Number.toInteger()</code></strong> 用来将参数转换成整数,但该方法的实现已被移除.</p> + +<p>如果参数是 {{jsxref("Global_Objects/NaN", "NaN")}}, {{jsxref("Global_Objects/null", "null")}} 或 {{jsxref("Global_Objects/undefined", "undefined")}}, 会返回0 . 如果参数值是true返回1,false的话返回<span style="line-height: 19.0909080505371px;">0</span>.</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>Number.toInteger(<var>number</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>number</code></dt> + <dd>将被转换成整数的参数.</dd> +</dl> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example:_Using_toInteger" name="Example:_Using_toInteger">例子: 使用 <code>toInteger()方法</code></h3> + +<pre class="brush: js">Number.toInteger(0.1); // 0 +Number.toInteger(1); // 1 +Number.toInteger(Math.PI); // 3 +Number.toInteger(null); // 0 +</pre> + +<h2 id="Specifications" name="Specifications">Specifications</h2> + +<ul> + <li><code>Number.toInteger()</code> 是<span style="line-height: 19.0909080505371px;">ECMAScript 6 起草的一部分</span>, 但在2013-8-23的Draft Rev 17 中被移除 .</li> +</ul> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>Firefox 16 to 32</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>Firefox 16 to 32</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">See also</h2> + +<ul> + <li>归属于 {{jsxref("Global_Objects/Number", "Number")}} 对象.</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tolocalestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tolocalestring/index.html new file mode 100644 index 0000000000..e907f810c9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/tolocalestring/index.html @@ -0,0 +1,165 @@ +--- +title: Number.prototype.toLocaleString() +slug: Web/JavaScript/Reference/Global_Objects/Number/toLocaleString +tags: + - JavaScript + - 原型 + - 国际化 + - 数字 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toLocaleString +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<p><code><strong>toLocaleString()</strong></code> 方法返回这个数字在特定语言环境下的表示字符串。</p> + +<p>新的 <code>locales</code> 和 <code>options</code> 参数让应用程序可以指定要进行格式转换的语言,并且定制函数的行为。在旧的实现中,会忽略 <code>locales</code> 和 <code>options</code> 参数,使用的语言环境和返回的字符串的形式完全取决于实现方式。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>numObj</em>.toLocaleString(</code><code>[locales [, options]])</code></pre> + +<h3 id="参数">参数</h3> + +<p>查阅<a href="#Browser_Compatibility">浏览器兼容性</a>部分,了解哪些浏览器支持 <code>locales</code> 和 <code>options</code> 参数,通过<a href="#Checking_for_support_for_locales_and_options_arguments">示例: 检查 <code>locales</code> 和 <code>options</code> 参数的支持</a>了解特征检测。</p> + +<div class="note"> +<p><strong>注意:</strong> ECMAScript 国际化 API,在 Firefox 29 中得以实施,增加了 <code>locales</code> 参数的 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString"><code>Number.toLocaleString</code></a> 方法。如果参数为 <code>undefined</code>,此方法返回本地操作系统指定的位数,而 Firefox 的早期版本中返回<a href="https://en.wikipedia.org/wiki/Arabic_numerals">阿拉伯语</a>数字。这一变化已被报告为向后影响的兼容性问题并可能会被尽快修复。({{ bug(999003) }})</p> +</div> + +<div>{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat', '参数')}}</div> + +<h3 id="返回值">返回值</h3> + +<p>返回一个语言环境下的表示字符串。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_toLocaleString">使用 <code>toLocaleString</code></h3> + +<p>在没有指定区域的基本使用时,返回使用默认的语言环境和默认选项格式化的字符串。</p> + +<pre class="brush: js">var number = 3500; + +console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale +</pre> + +<h3 id="检查_locales_和_options_参数的支持">检查 <code>locales</code> 和 <code>options</code> 参数的支持</h3> + +<p><code>locales</code> 和 <code>options</code> 参数目前还不是所有浏览器都支持的。在 ES5.1 和更新的实现中检查支持情况,可以依靠使用非法参数时规定抛出的 {{jsxref("Global_Objects/RangeError", "RangeError")}} 异常:</p> + +<pre class="brush: js">function toLocaleStringSupportsLocales() { + var number = 0; + try { + number.toLocaleString('i'); + } catch (e) { + return e.name === 'RangeError'; + } + return false; +} +</pre> + +<p>早于 ES5.1 的实现中,如果带参数调用 <code>toLocaleString</code> 并不会抛出范围异常。</p> + +<p>在所有宿主环境下,包括那些支持比 ed 5.1 还早的 ECMA-262 的环境,都能有效检测的方法是直接检测 ECMA-402 中的其它特性,它指定 <code>Number.prototype.toLocaleString</code> 需要支持地区选项:</p> + +<pre class="brush: js">function toLocaleStringSupportsOptions() { + return !!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function'); +} +</pre> + +<p>它测试全局的 <code>Intl</code> 对象,检测它不是 <code>null</code> 并且有 <code>NumberFormat</code> 的方法。</p> + +<h3 id="使用_locales">使用 <code>locales</code></h3> + +<p>这个示例展示了不同地区数字格式的差异。为了设置你的应用程序界面下使用的语言格式,请确保使用 <code>locales</code> 参数指定了使用的语言(可能还有一些备用语言):</p> + +<pre class="brush: js">var number = 123456.789; + +// 德国使用逗号作为小数分隔符,分位周期为千位 +console.log(number.toLocaleString('de-DE')); +// → 123.456,789 + +// 在大多数阿拉伯语国家使用<a href="https://en.wikipedia.org/wiki/Eastern_Arabic_numerals">阿拉伯语</a>数字 +console.log(number.toLocaleString('ar-EG')); +// → ١٢٣٤٥٦٫٧٨٩ + +// 印度使用千位/拉克(十万)/克若尔(千万)分隔 +console.log(number.toLocaleString('en-IN')); +// → 1,23,456.789 + +// nu 扩展字段要求编号系统,e.g. 中文十进制 +console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec')); +// → 一二三,四五六.七八九 + +// 当请求不支持的语言时,例如巴厘语,加入一个备用语言,比如印尼语 +console.log(number.toLocaleString(['ban', 'id'])); +// → 123.456,789 +</pre> + +<h3 id="使用_options">使用 <code>options</code></h3> + +<p>通过 <code>toLocaleString</code> 返回的结果可以通过 <code>options</code> 参数进行定制:</p> + +<pre class="brush: js">var number = 123456.789; + +// 要求货币格式 +console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' })); +// → 123.456,79 € + +// 日元不使用小数位 +console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' })) +// → ¥123,457 + +// 限制三位有效数字 +console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 })); +// → 1,23,000 +</pre> + +<h2 id="性能">性能</h2> + +<p>当格式化大量数字时,最好建立一个 {{jsxref("Global_Objects/NumberFormat", "NumberFormat")}} 对象并且使用它提供的 {{jsxref("NumberFormat.format")}} 方法。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注解</th> + </tr> + <tr> + <td>ECMAScript 3rd Edition. Implemented in JavaScript 1.5</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.3', 'Number.prototype.toLocaleString')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.tolocalestring', 'Number.prototype.toLocaleString')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td><a href="http://www.ecma-international.org/ecma-402/1.0/#sec-13.2.1">ECMAScript Internationalization API Specification, 1<sup>st</sup> Edition (ECMA-402)</a></td> + <td>Standard</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Number.toLocaleString")}}</p> + +<div id="compat-mobile"> </div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/toprecision/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/toprecision/index.html new file mode 100644 index 0000000000..daa0fa0bc8 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/toprecision/index.html @@ -0,0 +1,133 @@ +--- +title: Number.prototype.toPrecision() +slug: Web/JavaScript/Reference/Global_Objects/Number/toPrecision +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toPrecision +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><code><strong>toPrecision()</strong></code> 方法以指定的精度返回该数值对象的字符串表示。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>numObj</em>.toPrecision(<em>precision</em>)</code></pre> + +<h3 id="Parameter" name="Parameter">参数</h3> + +<dl> + <dt>precision</dt> + <dd>可选。一个用来指定有效数个数的整数。</dd> +</dl> + +<h3 id="Returns" name="Returns">返回值</h3> + +<p>以定点表示法或指数表示法表示的一个数值对象的字符串表示,四舍五入到 <code>precision</code> 参数指定的显示数字位数。查看 {{jsxref("Number.prototype.toFixed()")}} 方法关于四舍五入的讨论,同样应用于 <code>toPrecision</code> 方法。</p> + +<p>如果忽略 <code>precision</code> 参数,则该方法表现类似于 {{jsxref("Number.prototype.toString()")}}。如果该参数是一个非整数值,将会向下舍入到最接近的整数。</p> + +<h3 id="异常">异常</h3> + +<dl> + <dt>{{jsxref("Global_Objects/RangeError", "RangeError")}}</dt> + <dd>如果 <em>precison</em> 参数不在 1 和 100 (包括)之间,将会抛出一个 <code>RangeError</code> 。执行环境也可以支持更大或更小的范围。ECMA-262 只需要最多 21 位显示数字。</dd> +</dl> + +<h2 id="Example" name="Example">示例</h2> + +<pre class="brush: js">var numObj = 5.123456; +console.log("numObj.toPrecision() is " + numObj.toPrecision()); //输出 5.123456 +console.log("numObj.toPrecision(5) is " + numObj.toPrecision(5)); //输出 5.1235 +console.log("numObj.toPrecision(2) is " + numObj.toPrecision(2)); //输出 5.1 +console.log("numObj.toPrecision(1) is " + numObj.toPrecision(1)); //输出 5 + +// 注意:在某些情况下会以指数表示法返回 +console.log((1234.5).toPrecision(2)); // "1.2e+3" +</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>ECMAScript 3rd Edition. Implemented in JavaScript 1.5</td> + <td>Standard</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.7', 'Number.prototype.toPrecision')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.toprecision', 'Number.prototype.toPrecision')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_Also" name="See_Also">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toFixed()")}}</li> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toString()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tosource/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tosource/index.html new file mode 100644 index 0000000000..3ed14e1397 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/tosource/index.html @@ -0,0 +1,80 @@ +--- +title: Number.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/Number/toSource +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toSource +--- +<div> + {{JSRef("Global_Objects", "Number")}} {{ Non-standard_header() }}</div> +<h2 id="Summary" name="Summary">概述</h2> +<p><code><strong>toSource()</strong></code> 方法返回该对象源码的字符串表示。</p> +<h2 id="Syntax" name="Syntax">语法</h2> +<pre class="syntaxbox">numObj.toSource(); +Number.toSource(); +</pre> +<h3 id="Parameters" name="Parameters">参数</h3> +<p>无</p> +<h2 id="Description" name="Description">描述</h2> +<p><code>toSource()</code> 方法返回下列值:</p> + +<ul> + <li>对于内置 <code>Number</code> 对象, <code>toSource()</code> 返回下面字符串,用来指明源码是不可见的。</li> +</ul> +<pre class="eval">function Number() { [native code] }</pre> +<ul> + <li>对于 <code>Number</code> 对象的实例,<code>toSource()</code> 返回一个表示源码的字符串。</li> +</ul> +<p>该方法通常被 JavaScript 内部调用,而不是在代码中显示调用。</p> +<h2 id="规范">规范</h2> +<p>不是任何标准的一部分。在 JavaScript 1.3 被实现。</p> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<h2 id="See_Also" name="See_Also">相关链接</h2> +<ul> + <li>{{jsxref("Object.prototype.toSource()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html new file mode 100644 index 0000000000..30fdc17406 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/tostring/index.html @@ -0,0 +1,149 @@ +--- +title: Number.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/Number/toString +tags: + - Bug + - Number.prototype.toString() + - Tips +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toString +--- +<p>{{JSRef}}</p> + +<p><strong><code>toString()</code></strong> 方法返回指定 {{jsxref("Number")}} 对象的字符串表示形式。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre><code><var>numObj</var>.toString([<var>radix</var>])</code> +</pre> + +<h3 id="Parameter" name="Parameter">参数</h3> + +<dl> + <dt>radix</dt> + <dd>指定要用于数字到字符串的转换的基数(从2到36)。如果未指定 <samp>radix</samp> 参数,则默认值为 10。</dd> +</dl> + +<h3 id="异常信息">异常信息</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd> + <p>如果 <code>toString()</code> 的 radix 参数不在 2 到 36 之间,将会抛出一个 {{jsxref("RangeError")}}。</p> + </dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p>{{jsxref("Number")}} 对象覆盖了 {{jsxref("Object")}} 对象上的 <code>toString()</code> 方法,它不是继承的 {{jsxref("Object.prototype.toString()")}}。对于 {{jsxref( "Number")}} 对象,<code>toString()</code> 方法以指定的基数返回该对象的字符串表示。</p> + +<p>如果转换的基数大于10,则会使用字母来表示大于9的数字,比如基数为16的情况,则使用a到f的字母来表示10到15。</p> + +<p>如果基数没有指定,则使用 10。</p> + +<p>如果对象是负数,则会保留负号。即使radix是2时也是如此:返回的字符串包含一个负号(-)前缀和正数的二进制表示,<strong>不是</strong> 数值的二进制补码。</p> + +<p>进行数字到字符串的转换时,建议<strong>用小括号将要转换的目标括起来</strong>,防止出错。</p> + +<h2 id="Examples" name="Examples">例子</h2> + +<pre class="brush: js"><code>var count = 10; + +console.log(count.toString()); // 输出 '10' +console.log((17).toString()); // 输出 '17' +console.log((17.2).toString()); // 输出 '17.2' + +var x = 6; + +console.log(x.toString(2)); // 输出 '110' +console.log((254).toString(16)); // 输出 'fe' + +console.log((-10).toString(2)); // 输出 '-1010' +console.log((-0xff).toString(2)); // 输出 '-11111111'</code> +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.2', 'Number.prototype.tostring')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.tostring', 'Number.prototype.tostring')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toFixed()")}}</li> + <li>{{jsxref("Number.prototype.toExponential()")}}</li> + <li>{{jsxref("Number.prototype.toPrecision()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/number/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/number/valueof/index.html new file mode 100644 index 0000000000..81168e6ea0 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/number/valueof/index.html @@ -0,0 +1,122 @@ +--- +title: Number.prototype.valueOf() +slug: Web/JavaScript/Reference/Global_Objects/Number/valueOf +tags: + - JavaScript + - Method + - Number + - valueOf() +translation_of: Web/JavaScript/Reference/Global_Objects/Number/valueOf +--- +<div>{{JSRef("Global_Objects", "Number")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><strong><code>valueOf()</code></strong> 方法返回一个被 {{jsxref("Global_Objects/Number", "Number")}} 对象包装的原始值。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><var>numObj</var>.valueOf()</code></pre> + +<h3 id="Parameters" name="Parameters">返回值</h3> + +<p>表示指定 {{jsxref("Number")}} 对象的原始值的数字</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>该方法通常是由 JavaScript 引擎在内部隐式调用的,而不是由用户在代码中显式调用的。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<pre class="brush: js">var numObj = new Number(10); +console.log(typeof numObj); // object + +var num = numObj.valueOf(); +console.log(num); // 10 +console.log(typeof num); // number +</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>ECMAScript 1st Edition.</td> + <td>Standard</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.7.4.4', 'Number.prototype.valueOf')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-number.prototype.valueof', 'Number.prototype.valueOf')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("Number.prototype.toSource()")}}</li> +</ul> |
