mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
197 lines
17 KiB
HTML
197 lines
17 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p> JavaScript 的 <strong><code>Number</code></strong> 对象是经过封装的能让你处理数字值的对象。<code>Number</code> 对象由 <code>Number()</code> 构造器创建。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript">new Number(value);</code></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>如果参数无法被转换为数字,则返回 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字(Not-A-Number)。"><code>NaN</code></a>。</li>
|
||
<li>在非构造器上下文中 (如:没有 <a href="Reference/Operators/new" title="new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。"><code>new</code></a> 操作符),<code>Number</code> 能被用来执行类型转换。</li>
|
||
</ul>
|
||
<h2 id="Properties" name="Properties">属性</h2>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/Number/EPSILON" title="Number.EPSILON 属性表示 1 与Number可表示的大于 1 的最小的浮点数之间的差值。"><code>Number.EPSILON</code></a></dt>
|
||
<dd>两个可表示(representable)数之间的最小间隔。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/MAX_SAFE_INTEGER" title="Number.MAX_SAFE_INTEGER 常量表示在 JavaScript 中最大的安全整数(maxinum safe integer)(253 - 1)。"><code>Number.MAX_SAFE_INTEGER</code></a></dt>
|
||
<dd>JavaScript 中最大的安全整数 (<code>2<sup>53</sup> - 1</code>)。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/MAX_VALUE" title="Number.MAX_VALUE 属性表示在 JavaScript 里所能表示的最大数值。"><code>Number.MAX_VALUE</code></a></dt>
|
||
<dd>能表示的最大正数。最小的负数是 <code>-MAX_VALUE</code>。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/MIN_SAFE_INTEGER" title="Number.MIN_SAFE_INTEGER 代表在 JavaScript中最小的安全的integer型数字 (-(253 - 1))."><code>Number.MIN_SAFE_INTEGER</code></a></dt>
|
||
<dd>JavaScript 中最小的安全整数 (<code>-(2<sup>53</sup> - 1)</code>).</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/MIN_VALUE" title="Number.MIN_VALUE 属性表示在 JavaScript 中所能表示的最小的正值。"><code>Number.MIN_VALUE</code></a></dt>
|
||
<dd>能表示的最小正数即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 <code>-MIN_VALUE</code>。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/NaN" title="Number.NaN 表示“非数字”(Not-A-Number)。和 NaN 相同。"><code>Number.NaN</code></a></dt>
|
||
<dd>特殊的“非数字”值。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/NEGATIVE_INFINITY" title="Number.NEGATIVE_INFINITY 属性表示负无穷大。"><code>Number.NEGATIVE_INFINITY</code></a></dt>
|
||
<dd>特殊的负无穷大值,在溢出时返回该值。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/POSITIVE_INFINITY" title="Number.POSITIVE_INFINITY 属性表示正无穷大。"><code>Number.POSITIVE_INFINITY</code></a></dt>
|
||
<dd>特殊的正无穷大值,在溢出时返回改值。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/prototype" title="Number.prototype 属性表示 Number 构造函数的原型。"><code>Number.prototype</code></a></dt>
|
||
<dd>Number 对象上允许的额外属性。</dd>
|
||
</dl>
|
||
<h2 id="Methods" name="Methods">方法</h2>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/Number/isNaN" title="Number.isNaN() 方法确定传递的值是否为 NaN和其类型是 Number。它是原始的全局isNaN()的更强大的版本。"><code>Number.isNaN()</code></a></dt>
|
||
<dd>确定传递的值是否是 NaN。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/isFinite" title="Number.isFinite() 方法用来检测传入的参数是否是一个有穷数(finite number)。"><code>Number.isFinite()</code></a></dt>
|
||
<dd>确定传递的值类型及本身是否是有限数。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/isInteger" title="Number.isInteger() 方法用来判断给定的参数是否为整数。"><code>Number.isInteger()</code></a></dt>
|
||
<dd>确定传递的值类型是“number”,且是整数。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/isSafeInteger" title="Number.isSafeInteger() 方法用来判断传入的参数值是否是一个“安全整数”(safe integer)。一个安全整数是一个符合下面条件的整数:"><code>Number.isSafeInteger()</code></a></dt>
|
||
<dd>确定传递的值是否为安全整数 ( -<code>(2<sup>53</sup> - 1)</code> 至 <code>2<sup>53</sup> - 1之间</code>)。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/toInteger" title="Number.toInteger() 用来将参数转换成整数,但该方法的实现已被移除."><code>Number.toInteger()</code></a> <span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span></dt>
|
||
<dd>计算传递的值并将其转换为整数 (或无穷大)。</dd>
|
||
</dl>
|
||
<div>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/Number/parseFloat" title="Number.parseFloat() 方法可以把一个字符串解析成浮点数。该方法与全局的 parseFloat() 函数相同,并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。"><code>Number.parseFloat()</code></a></dt>
|
||
<dd>和全局对象 <a href="Reference/Global_Objects/parseFloat" title="parseFloat() 函数解析一个字符串参数并返回一个浮点数。"><code>parseFloat()</code></a> 一样。</dd>
|
||
<dt><a href="Reference/Global_Objects/Number/parseInt" title="Number.parseInt() 方法可以根据给定的进制数把一个字符串解析成整数。"><code>Number.parseInt()</code></a></dt>
|
||
<dd>和全局对象 <a href="Reference/Global_Objects/parseInt" title="parseInt() 函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。"><code>parseInt()</code></a> 一样。</dd>
|
||
</dl>
|
||
</div>
|
||
<h2 id="Number_instances" name="Number_instances"><code>Number</code> 实例</h2>
|
||
<p>所有 <code>Number</code> 实例都继承自 <a href="Reference/Global_Objects/Number/prototype" title="Number.prototype 属性表示 Number 构造函数的原型。"><code>Number.prototype</code></a>。<code>被修改的 Number</code> 构造器的原型对象对全部 <code>Number</code> 实例都生效。</p>
|
||
<h3 id="Methods_of_number_instance" name="Methods_of_number_instance">方法</h3>
|
||
<div><dl>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential" title="The toExponential() method returns a string representing the Number object in exponential notation."><code>Number.prototype.toExponential()</code></a></dt>
|
||
<dd>Returns a string representing the number in exponential notation.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed" title="The toFixed() method formats a number using fixed-point notation."><code>Number.prototype.toFixed()</code></a></dt>
|
||
<dd>Returns a string representing the number in fixed-point notation.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString" title="The toLocaleString() method returns a string with a language-sensitive representation of this number."><code>Number.prototype.toLocaleString()</code></a></dt>
|
||
<dd>Returns a string with a language sensitive representation of this number. Overrides the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString" title="The toLocaleString() method returns a string representing the object. This method is meant to be overridden by derived objects for locale-specific purposes."><code>Object.prototype.toLocaleString()</code></a> method.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision" title="The toPrecision() method returns a string representing the Number object to the specified precision."><code>Number.prototype.toPrecision()</code></a></dt>
|
||
<dd>Returns a string representing the number to a specified precision in fixed-point or exponential notation.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toSource" title="The toSource() method returns a string representing the source code of the object."><code>Number.prototype.toSource()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
|
||
<dd>Returns an object literal representing the specified <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number" title="The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor. A primitive type object number is created using the Number() function."><code>Number</code></a> object; you can use this value to create a new object. Overrides the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toSource" title="The toSource() method returns a string representing the source code of the object."><code>Object.prototype.toSource()</code></a> method.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString" title="The toString() method returns a string representing the specified Number object."><code>Number.prototype.toString()</code></a></dt>
|
||
<dd>Returns a string representing the specified object in the specified radix (base). Overrides the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString" title="The toString() method returns a string representing the object."><code>Object.prototype.toString()</code></a> method.</dd>
|
||
<dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf" title="The valueOf() method returns the wrapped primitive value of a Number object."><code>Number.prototype.valueOf()</code></a></dt>
|
||
<dd>Returns the primitive value of the specified object. Overrides the <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf" title="The valueOf() method returns the primitive value of the specified object."><code>Object.prototype.valueOf()</code></a> method.</dd>
|
||
</dl></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><code class="language-javascript">var biggestNum = Number.MAX_VALUE;
|
||
var smallestNum = Number.MIN_VALUE;
|
||
var infiniteNum = Number.POSITIVE_INFINITY;
|
||
var negInfiniteNum = Number.NEGATIVE_INFINITY;
|
||
var notANum = Number.NaN;
|
||
</code></pre>
|
||
<h3 id="整数类型的范围">整数类型的范围</h3>
|
||
<p>下例展示了<code>Number</code>对象所能表示的最大和最小整数 (详情请参阅 ECMAScript standard, chapter <em>8.5 The Number Type</em>):</p>
|
||
<pre><code class="language-javascript">var biggestInt = 9007199254740992;
|
||
var smallestInt = -9007199254740992;</code></pre>
|
||
<p>在解析序列化的JSON时,如果JSON解析器将它们强制转换为Number类型,那么超出此范围的整数值可能会被破坏。在工作中使用<a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a> 类型代替,是一个可行的解决方案。</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><code class="language-javascript">var d = new Date("December 17, 1995 03:24:00");
|
||
print(Number(d));
|
||
</code></pre>
|
||
<p>这将输出 "819199440000"。</p>
|
||
<h3 id="转换数字字符串为数字">转换数字字符串为数字</h3>
|
||
<pre><code class="language-javascript">Number("123") // 123
|
||
Number("") // 0
|
||
Number("0x11") // 17
|
||
Number("0b11") // 3
|
||
Number("0o11") // 9
|
||
Number("foo") // NaN
|
||
Number("100a") // NaN</code></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><a class="external" href="https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf" hreflang="en" lang="en" rel="noopener" title="ECMAScript 1st Edition (ECMA-262)">ECMAScript 1st Edition (ECMA-262)</a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>初始定义。 实现于 JavaScript 1.1.</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.7" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Number</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-number-objects" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Number</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>新增了方法和属性: <a href="Reference/Global_Objects/Number/EPSILON" title="Number.EPSILON 属性表示 1 与Number可表示的大于 1 的最小的浮点数之间的差值。"><code>EPSILON</code></a>, <a href="Reference/Global_Objects/Number/isFinite" title="Number.isFinite() 方法用来检测传入的参数是否是一个有穷数(finite number)。"><code>isFinite</code></a>, <a href="Reference/Global_Objects/Number/isInteger" title="Number.isInteger() 方法用来判断给定的参数是否为整数。"><code>isInteger</code></a>, <a href="Reference/Global_Objects/Number/isNaN" title="Number.isNaN() 方法确定传递的值是否为 NaN和其类型是 Number。它是原始的全局isNaN()的更强大的版本。"><code>isNaN</code></a>, <a href="Reference/Global_Objects/Number/parseFloat" title="Number.parseFloat() 方法可以把一个字符串解析成浮点数。该方法与全局的 parseFloat() 函数相同,并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。"><code>parseFloat</code></a>, <a href="Reference/Global_Objects/Number/parseInt" title="Number.parseInt() 方法可以根据给定的进制数把一个字符串解析成整数。"><code>parseInt</code></a></td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-number-objects" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Number</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></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 (WebKit)</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Phone</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_also" name="See_also">参阅</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字(Not-A-Number)。"><code>NaN</code></a></li>
|
||
<li>全局对象 <a href="Reference/Global_Objects/Math" title="Math 是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a> </li>
|
||
</ul>
|
||
</article> |