73 lines
3.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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>
<h2 id="描述">描述</h2>
<pre><code class="language-javascript">RangeError: precision {0} out of range (Firefox)
RangeError: toExponential() argument must be between 0 and 20 (Chrome)
RangeError: toFixed() digits argument must be between 0 and 20 (Chrome)
RangeError: toPrecision() argument must be between 1 and 21 (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a></p>
<h2 id="什么地方出错了?">什么地方出错了?</h2>
<p>以下的某个方法传入了一个超出精度范围的参数:</p>
<ul>
<li><a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>Number.prototype.toExponential()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>Number.prototype.toFixed()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>Number.prototype.toPrecision()</code></a></li>
</ul>
<p>通常这些方法允许的参数范围介于0和20或21之间。需要注意的是ECMAScript标准是允许扩展这个范围的。</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Method</th>
<th scope="col">Firefox (SpiderMonkey)</th>
<th scope="col">Chrome, Opera (V8)</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>Number.prototype.toExponential()</code></a></td>
<td>0 to 100</td>
<td>0 to 20</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>Number.prototype.toFixed()</code></a></td>
<td>-20 to 100</td>
<td>0 to 20</td>
</tr>
<tr>
<td><a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>Number.prototype.toPrecision()</code></a></td>
<td>1 to 100</td>
<td>1 to 21</td>
</tr>
</tbody>
</table>
<h2 id="示例">示例</h2>
<h3 id="错误的示例">错误的示例</h3>
<pre><code class="language-js example-bad">77.1234.toExponential(-1); // RangeError
77.1234.toExponential(101); // RangeError
2.34.toFixed(-100); // RangeError
2.34.toFixed(1001); // RangeError
1234.5.toPrecision(-1); // RangeError
1234.5.toPrecision(101); // RangeError
</code></pre>
<h3 id="正确的示例">正确的示例</h3>
<pre><code class="language-js example-good">77.1234.toExponential(4); // 7.7123e+1
77.1234.toExponential(2); // 7.71e+1
2.34.toFixed(1); // 2.3
2.35.toFixed(1); // 2.4 (note that it rounds up in this case)
5.123456.toPrecision(5); // 5.1235
5.123456.toPrecision(2); // 5.1
5.123456.toPrecision(1); // 5
</code></pre>
<h2 id="相关页面">相关页面</h2>
<ul>
<li><a href="Reference/Global_Objects/Number/toExponential" title="toExponential() 方法以指数表示法返回该数值字符串表示形式。"><code>Number.prototype.toExponential()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toFixed" title="toFixed() 方法使用定点表示法来格式化一个数。"><code>Number.prototype.toFixed()</code></a></li>
<li><a href="Reference/Global_Objects/Number/toPrecision" title="toPrecision() 方法以指定的精度返回该数值对象的字符串表示。"><code>Number.prototype.toPrecision()</code></a></li>
</ul>
</article>