Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 错误参考
  5. RangeError: precision is out of range

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

RangeError: precision is out of range

描述

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)

错误类型

RangeError

什么地方出错了?

以下的某个方法传入了一个超出精度范围的参数:

通常这些方法允许的参数范围介于 0 和 20(或 21)之间。需要注意的是,ECMAScript 标准是允许扩展这个范围的。

MethodFirefox (SpiderMonkey)Chrome, Opera (V8)
Number.prototype.toExponential()0 to 1000 to 20
Number.prototype.toFixed()-20 to 1000 to 20
Number.prototype.toPrecision()1 to 1001 to 21

示例

错误的示例

js
(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

正确的示例

js
(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

相关页面

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp