Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1028744 of RangeError: precision is out of range

  • Revision slug: Web/JavaScript/Reference/Errors/Precision_range
  • Revision title: Precision range
  • Revision id: 1028744
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

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)

Error type

{{jsxref("RangeError")}}

What went wrong?

You were using an out of range precision argument in one of these methods:

  • {{jsxref("Number.prototype.toExponential()")}}
  • {{jsxref("Number.prototype.toFixed()")}}
  • {{jsxref("Number.prototype.toPrecision()")}}

The allowed range for these methods is usually between 0 and 20 (or 21). However, the ECMAScript specification allows to extend this range.

Method Firefox (SpiderMonkey) Chrome, Opera (V8)
{{jsxref("Number.prototype.toExponential()")}} 0 to 100 0 to 20
{{jsxref("Number.prototype.toFixed()")}} -20 to 100 0 to 20
{{jsxref("Number.prototype.toPrecision()")}} 1 to 100 1 to 21

Examples

Invalid cases

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

Valid cases

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

See also

  • {{jsxref("Number.prototype.toExponential()")}}
  • {{jsxref("Number.prototype.toFixed()")}}
  • {{jsxref("Number.prototype.toPrecision()")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
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)
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("RangeError")}}</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>You were using an out of range precision argument in one of these methods:</p>

<ul>
 <li>{{jsxref("Number.prototype.toExponential()")}}</li>
 <li>{{jsxref("Number.prototype.toFixed()")}}</li>
 <li>{{jsxref("Number.prototype.toPrecision()")}}</li>
</ul>

<p>The allowed range for these methods is usually between 0 and 20 (or 21). However, the ECMAScript specification allows to extend this range.</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>{{jsxref("Number.prototype.toExponential()")}}</td>
   <td>0 to 100</td>
   <td>0 to 20</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.prototype.toFixed()")}}</td>
   <td>-20 to 100</td>
   <td>0 to 20</td>
  </tr>
  <tr>
   <td>{{jsxref("Number.prototype.toPrecision()")}}</td>
   <td>1 to 100</td>
   <td>1 to 21</td>
  </tr>
 </tbody>
</table>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Invalid cases</h3>

<pre class="brush: 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
</pre>

<h3 id="Valid_cases">Valid cases</h3>

<pre class="brush: 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
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Number.prototype.toExponential()")}}</li>
 <li>{{jsxref("Number.prototype.toFixed()")}}</li>
 <li>{{jsxref("Number.prototype.toPrecision()")}}</li>
</ul>
Revert to this revision