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.

RangeError: precision is out of range

我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!

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

RangeError

What went wrong?

There was an out of range precision argument in one of these methods:

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)
Number.prototype.toExponential() 0 to 100 0 to 20
Number.prototype.toFixed() -20 to 100 0 to 20
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

文件標籤與貢獻者

 此頁面的貢獻者: fscholz
 最近更新: fscholz,