mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-05-16 10:29:58 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
<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 class="syntaxbox">parseInt(<em>string</em>, <em>radix</em>);</pre>
|
||||
<pre><code class="language-javascript">parseInt(<em>string</em>, <em>radix</em>);</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt><code>string</code></dt>
|
||||
@@ -24,7 +24,7 @@
|
||||
<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 => 1*5^2 + 2*5^1 + 3*5^0 = 38</pre>
|
||||
<pre>parseInt('123', 5) // 将'123'看作5进制数,返回十进制数38 => 1*5^2 + 2*5^1 + 3*5^0 = 38</code></pre>
|
||||
<p> </p>
|
||||
<h2 id="描述">描述</h2>
|
||||
<p><code>parseInt</code> 函数将其第一个参数转换为字符串,解析它,并返回一个整数或NaN。如果不是NaN,返回的值将是作为指定基数(基数)中的数字的第一个参数的整数。</p>
|
||||
@@ -47,7 +47,7 @@
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="例子:使用_parseInt">例子:使用 <code>parseInt</code></h3>
|
||||
<p>以下例子均返回<code>15</code>:</p>
|
||||
<pre class="brush: js">parseInt("0xF", 16);
|
||||
<pre><code class="language-javascript">parseInt("0xF", 16);
|
||||
parseInt("F", 16);
|
||||
parseInt("17", 8);
|
||||
parseInt(021, 8);
|
||||
@@ -59,13 +59,13 @@ parseInt("1111", 2);
|
||||
parseInt("15 * 3", 10);
|
||||
parseInt("15e2", 10);
|
||||
parseInt("15px", 10);
|
||||
parseInt("12", 13);</pre>
|
||||
parseInt("12", 13);</code></pre>
|
||||
<p>以下例子均返回 <code>NaN</code>:</p>
|
||||
<pre class="brush: js">parseInt("Hello", 8); // 根本就不是数值
|
||||
<pre><code class="language-javascript">parseInt("Hello", 8); // 根本就不是数值
|
||||
parseInt("546", 2); // 除了“0、1”外,其它数字都不是有效二进制数字
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>以下例子均返回 <code>-15</code>:</p>
|
||||
<pre class="brush: js">parseInt("-F", 16);
|
||||
<pre><code class="language-javascript">parseInt("-F", 16);
|
||||
parseInt("-0F", 16);
|
||||
parseInt("-0XF", 16);
|
||||
parseInt(-15.1, 10);
|
||||
@@ -74,21 +74,21 @@ parseInt(" -15", 10);
|
||||
parseInt("-1111", 2);
|
||||
parseInt("-15e1", 10);
|
||||
parseInt("-12", 13);
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>下例中全部返回 <code>4</code>:</p>
|
||||
<pre class="brush: js">parseInt(4.7, 10);
|
||||
<pre><code class="language-javascript">parseInt(4.7, 10);
|
||||
parseInt(4.7 * 1e22, 10); // 非常大的数值变成 4
|
||||
parseInt(0.00000000000434, 10); // 非常小的数值变成 4</pre>
|
||||
parseInt(0.00000000000434, 10); // 非常小的数值变成 4</code></pre>
|
||||
<p>下面的例子返回 <code>224</code></p>
|
||||
<pre class="brush: js">parseInt("0e0",16);</pre>
|
||||
<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 class="brush: js">parseInt("0e0");
|
||||
<pre><code class="language-javascript">parseInt("0e0");
|
||||
// 0
|
||||
|
||||
parseInt("08");
|
||||
// 0, '8' 不是八进制数字.
|
||||
</pre>
|
||||
</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>
|
||||
@@ -96,7 +96,7 @@ parseInt("08");
|
||||
<p>直至2013年,很多实现环境并没有采取新的规范所规定的做法, 而且由于必须兼容旧版的浏览器,所以<strong>永远都要明确给出radix参数的值.</strong></p>
|
||||
<h2 id="一个更严格的解析函数" style="margin-bottom: 20px; line-height: 30px;">一个更严格的解析函数</h2>
|
||||
<p>有时采用一个更严格的方法来解析整型值很有用。此时可以使用正则表达式:</p>
|
||||
<pre class="brush: js">filterInt = function (value) {
|
||||
<pre><code class="language-javascript">filterInt = function (value) {
|
||||
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
|
||||
return Number(value);
|
||||
return NaN;
|
||||
@@ -110,7 +110,7 @@ console.log(filterInt('421e+0')); // NaN
|
||||
console.log(filterInt('421hop')); // NaN
|
||||
console.log(filterInt('hop1.61803398875')); // NaN
|
||||
console.log(filterInt('1.61803398875')); // NaN
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user