2019-04-21 11:50:48 +08:00

238 lines
16 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div>
<div>
<div></div>
</div>
</div>
<p><strong>parseInt() </strong>函数解析一个字符串参数,并返回一个指定基数的整数 (数学系统的基础)。</p>
<p> </p>
<p><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/globalprops-parseint.html" width="100%"></iframe></p>
<p>此交互式示例的代码存储在GitHub仓库中。如果你想参与交互式示例项目, 请克隆 <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> 之后给我们发一个pull请求。</p>
<p> </p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">parseInt(<em>string</em>, <em>radix</em>);</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>string</code></dt>
<dd>要被解析的值。如果参数不是一个字符串,则将其转换为字符串(使用  <code><a class="external" href="http://www.ecma-international.org/ecma-262/6.0/#sec-tostring" rel="noopener">ToString</a> </code>抽象操作)。字符串开头的空白符将会被忽略。</dd>
</dl>
<dl>
<dt><code>radix</code></dt>
<dd>一个介于2和36之间的整数(数学系统的基础),表示上述字符串的<strong>基数</strong>。比如参数"10"表示使用我们通常使用的十进制数值系统。<strong>始终指定此参数</strong>可以消除阅读该代码时的困惑并且保证转换结果可预测。当未指定基数时,不同的实现会产生不同的结果,通常将值默认为<strong>10</strong></dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>返回解析后的整数值。 如果被解析参数的第一个字符无法被转化成数值类型,则返回 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字Not-A-Number。"><code>NaN</code></a></p>
<p> </p>
<p>注意:<code>radix</code>参数为n 将会把第一个参数看作是一个数的n进制表示而返回的值则是十进制的。例如</p>
<pre>parseInt('123', 5) // 将'123'看作5进制数返回十进制数38 =&gt; 1*5^2 + 2*5^1 + 3*5^0 = 38</code></pre>
<p> </p>
<h2 id="描述">描述</h2>
<p><code>parseInt</code> 函数将其第一个参数转换为字符串解析它并返回一个整数或NaN。如果不是NaN返回的值将是作为指定基数基数中的数字的第一个参数的整数。</p>
<p>例如:<code>radix</code>参数为10 将会把第一个参数看作是一个数的十进制表示8 对应八进制16 对应十六进制,等等。基数大于 <code>10</code> 时,用字母表中的字母来表示大于<code> 9</code> 的数字。例如十六进制中,使用 <code>A </code><code>F</code></p>
<p>如果<code>parseInt</code>的字符不是指定基数中的数字,则忽略该字符和所有后续字符,并返回解析到该点的整数值。<code>parseInt</code>将数字截断为整数值。允许使用前导空格和尾随空格。</p>
<p>一些数中可能包含e字符例如6.022e23使用parseInt去截取包含e字符数值部分会造成难以预料的结果。例如</p>
<p>parseInt("6.022e23", 10);        // 返回 6<br/>
parseInt(6.022e2, 10);          // 返回 602</p>
<p>parseInt不应该用作 <a href="Reference/Global_Objects/Math/floor" title="Math.floor() 返回小于或等于一个给定数字的最大整数。"><code>Math.floor()</code></a>的替代品。</p>
<p>如果 <code>parseInt</code> 遇到了不属于<code>radix</code>参数所指定的基数中的字符那么该字符和其后的字符都将被忽略。接着返回已经解析的整数部分。<code>parseInt</code> 将截取整数部分。开头和结尾的空白符允许存在,会被忽略。</p>
<p>在基数为 <code>undefined</code>,或者基数为 0 或者没有指定的情况下JavaScript 作如下处理:</p>
<ul>
<li>如果字符串 <code>string</code> 以"0x"或者"0X"开头, 则基数是16 (16进制).</li>
<li>如果字符串 <code>string</code> 以"0"开头, 基数是8八进制或者10十进制那么具体是哪个基数由实现环境决定。<span style="background-color: #ffffff; color: #4d4e53; font-family: open sans,arial,sans-serif; font-size: 14px; line-height: 19.0909080505371px;">ECMAScript 5 规定使用10但是并不是所有的浏览器都遵循这个规定。因此<strong>永远都要明确给出radix参数的值</strong></span></li>
<li>如果字符串 <code>string</code> 以其它任何值开头则基数是10 (十进制)。</li>
</ul>
<p>如果第一个字符不能被转换成数字,<code>parseInt</code>返回<code>NaN</code></p>
<p>算术上, <code>NaN</code> 不是任何一个进制下的数。 你可以调用<a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN</code></a> 来判断 <code>parseInt</code> 是否返回 <code>NaN</code><code>NaN</code> 参与的数学运算其结果总是 <code>NaN</code></p>
<p>将整型数值以特定基数转换成它的字符串值可以使用 <code>intValue.toString(radix)</code>.</p>
<h2 id="示例">示例</h2>
<h3 id="例子使用_parseInt">例子:使用 <code>parseInt</code></h3>
<p>以下例子均返回<code>15</code>:</p>
<pre><code class="language-javascript">parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10); // parseInt(015, 10); 返回 15
parseInt(15.99, 10);
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15 * 3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);</code></pre>
<p>以下例子均返回 <code>NaN</code>:</p>
<pre><code class="language-javascript">parseInt("Hello", 8); // 根本就不是数值
parseInt("546", 2); // 除了“0、1”外其它数字都不是有效二进制数字
</code></pre>
<p>以下例子均返回 <code>-15</code></p>
<pre><code class="language-javascript">parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-15.1, 10);
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);
</code></pre>
<p>下例中全部返回 <code>4</code>:</p>
<pre><code class="language-javascript">parseInt(4.7, 10);
parseInt(4.7 * 1e22, 10); // 非常大的数值变成 4
parseInt(0.00000000000434, 10); // 非常小的数值变成 4</code></pre>
<p>下面的例子返回 <code>224</code></p>
<pre><code class="language-javascript">parseInt("0e0",16);</code></pre>
<h2 id="没有指定_radix_参数时的八进制解析" style="margin-bottom: 20px; line-height: 30px;">没有指定 <code>radix</code> 参数时的八进制解析</h2>
<p>尽管 ECMAScript 3 已经不赞成这种做法,且 ECMAScript 5 已经禁止了这种做法但是仍然有很多实现环境仍然把以 0 开头的数值字符串numeric string解释为一个八进制数。下面的例子可能返回八进制的结果也可能返回十进制的结果。<strong>总是指定一个基数radix可以避免这种不可靠的行为。</strong></p>
<pre><code class="language-javascript">parseInt("0e0");
// 0
parseInt("08");
// 0, '8' 不是八进制数字.
</code></pre>
<h3 id="ECMAScript_5_移除了八进制解析" style="line-height: 24px;">ECMAScript 5 移除了八进制解析</h3>
<p>ECMAScript 5 规范不再允许<code>parseInt</code>函数的<span style="line-height: 19.0909080505371px;">实现环境把以<code>0</code>字符开始的字符串作为八进制数值</span>。ECMAScript 5 陈述如下:</p>
<p>根据给定radix<code>parseInt</code>函数产生一个由字符串参数内容解析过来的整数值。字符串中开头的空白会被忽略。如果radix没有指定或者为0参数会被假定以10为基数来解析如果数值以字符对0x或0X开头会假定以16为基数来解析。</p>
<p>这与<span style="line-height: 19.0909080505371px;">ECMAScript 3有所不同ECMAScript 3仅仅是不提倡这种做法但并没有禁止这种做法。</span></p>
<p>直至2013年很多实现环境并没有采取新的规范所规定的做法, 而且由于必须兼容旧版的浏览器,所以<strong>永远都要明确给出radix参数的值.</strong></p>
<h2 id="一个更严格的解析函数" style="margin-bottom: 20px; line-height: 30px;">一个更严格的解析函数</h2>
<p>有时采用一个更严格的方法来解析整型值很有用。此时可以使用正则表达式:</p>
<pre><code class="language-javascript">filterInt = function (value) {
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
return Number(value);
return NaN;
}
console.log(filterInt('421')); // 421
console.log(filterInt('-421')); // -421
console.log(filterInt('+421')); // 421
console.log(filterInt('Infinity')); // Infinity
console.log(filterInt('421e+0')); // NaN
console.log(filterInt('421hop')); // NaN
console.log(filterInt('hop1.61803398875')); // NaN
console.log(filterInt('1.61803398875')); // NaN
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th>规范版本</th>
<th>规范状态</th>
<th>注解</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>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">parseInt</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-parseint-string-radix" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">parseInt</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>
<p> </p>
</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-parseint-string-radix" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">parseInt</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</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 style="line-height: 16px;">功能</th>
<th style="line-height: 16px;">谷歌浏览器</th>
<th style="line-height: 16px;">Edge</th>
<th style="line-height: 16px;">火狐浏览器 (Gecko)</th>
<th style="line-height: 16px;">IE浏览器</th>
<th style="line-height: 16px;">Opera浏览器</th>
<th style="line-height: 16px;">Safari</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: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>
<p><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></p>
</td>
</tr>
<tr>
<td>Parses leading-zero strings as decimal, not octal</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>21</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span>(in standards mode)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</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>Androidwebview</th>
<th>谷歌浏览器移动版</th>
<th>Edge</th>
<th>火狐浏览器移动版 (Gecko)</th>
<th>IE浏览器移动版</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>
<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>
<tr>
<td>Parses leading-zero strings as decimal, not octal</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>21</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/parseFloat" title="parseFloat() 函数解析一个字符串参数并返回一个浮点数。"><code>parseFloat()</code></a></li>
<li><a href="Reference/Global_Objects/Number/parseFloat" title="Number.parseFloat() 方法可以把一个字符串解析成浮点数。该方法与全局的 parseFloat() 函数相同并且处于 ECMAScript 6 规范中(用于全局变量的模块化)。"><code>Number.parseFloat()</code></a></li>
<li><a href="Reference/Global_Objects/Number/parseInt" title="Number.parseInt() 方法可以根据给定的进制数把一个字符串解析成整数。"><code>Number.parseInt()</code></a></li>
<li><a href="Reference/Global_Objects/isNaN" title="isNaN() 函数用来确定一个值是否为NaN 。注isNaN函数内包含一些非常有趣的规则你也可以通过ECMAScript 2015/ES6 中定义的Number.isNaN()或者 可以使用typeof 来判断该值是否为一个非数字。"><code>isNaN()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toString" title="toString() 方法返回指定 Number 对象的字符串表示形式。"><code>Number.toString()</code></a></li>
<li><a href="Reference/Global_Objects/Object/valueOf" title="valueOf() 方法返回指定对象的原始值。"><code>Object.valueOf</code></a></li>
</ul>
</article>