Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么toFixed()只接受0-20个数字_Javascript - Fatal编程技术网

Javascript 为什么toFixed()只接受0-20个数字

Javascript 为什么toFixed()只接受0-20个数字,javascript,Javascript,在javascript文档中 toFixed()只需要0-20秒,所以 123.1234567294239482379483749.toFixed(20) 是 并导致范围误差 123.1234567294239482379483749.toFixed(21) RangeError: toFixed() digits argument must be between 0 and 20 感谢ECMA文档中的,toFixed被指定为 注意第2点,这是该范围的原因 15.7.4.5 Number.

在javascript文档中

toFixed()只需要0-20秒,所以

123.1234567294239482379483749.toFixed(20)

并导致范围误差

123.1234567294239482379483749.toFixed(21)
RangeError: toFixed() digits argument must be between 0 and 20
感谢ECMA文档中的

,toFixed被指定为

注意第2点,这是该范围的原因

15.7.4.5 Number.prototype.toFixed (fractionDigits)
Return a string containing the number represented in fixed-point notation with fractionDigits digits
after the decimal point. If fractionDigits is undefined, 0 is assumed. Specifically, perform the
following steps:
1. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value
0).
2. If f < 0 or f > 20, throw a RangeError exception.
3. Let x be this number value.
4. If x is NaN, return the string "NaN".
5. Let s be the empty string.
6. If x ≥ 0, go to step 9.
7. Let s be "-".
8. Let x = –x.
9. If x ≥ 1021, let m = ToString(x) and go to step 20.
10. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as
possible. If there are two such n, pick the larger n.
11. If n = 0, let m be the string "0". Otherwise, let m be the string consisting of the digits of the
decimal representation of n (in order, with no leading zeroes).
12. If f = 0, go to step 20.
13. Let k be the number of characters in m.
14. If k > f, go to step 18.
15. Let z be the string consisting of f+1–k occurrences of the character ‘0’.
16. Let m be the concatenation of strings z and m.
17. Let k = f + 1.
18. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
19. Let m be the concatenation of the three strings a, ".", and b.
20. Return the concatenation of the strings s and m.
15.7.4.5 Number.prototype.toFixed(分形数字)
返回一个字符串,该字符串包含使用fractionDigits的定点表示法表示的数字
小数点后。如果未定义fractionDigits,则假定为0。具体来说,执行以下操作:
以下步骤:
1.设f为整数(分数位数)。(如果未定义fractionDigits,此步骤将生成值
0).
2.如果f<0或f>20,则抛出RangeError异常。
3.设x为该数值。
4.如果x是NaN,则返回字符串“NaN”。
5.让我们成为空字符串。
6.如果x≥ 0,转至步骤9。
7.让我们成为“-”。
8.设x=-x。
9如果x≥ 1021,让m=ToString(x)并转至步骤20。
10设n为整数,其精确数学值n÷10f–x尽可能接近于零
可能的如果有两个这样的n,选择较大的n。
11如果n=0,则m为字符串“0”。否则,设m是由
n的十进制表示法(按顺序,无前导零)。
12如果f=0,则转至步骤20。
13设k为m中的字符数。
14如果k>f,则转至步骤18。
15设z是由字符“0”的f+1–k个匹配项组成的字符串。
16设m是字符串z和m的串联。
17设k=f+1。
18设a为m的第一个k–f字符,b为m的剩余f字符。
19设m是三个字符串a、“.”和b的串联。
20返回字符串s和m的串联。

在我看来似乎是任意的,可能与JS的浮点精度有关?可能与ieee754的限制有关
15.7.4.5 Number.prototype.toFixed (fractionDigits)
Return a string containing the number represented in fixed-point notation with fractionDigits digits
after the decimal point. If fractionDigits is undefined, 0 is assumed. Specifically, perform the
following steps:
1. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value
0).
2. If f < 0 or f > 20, throw a RangeError exception.
3. Let x be this number value.
4. If x is NaN, return the string "NaN".
5. Let s be the empty string.
6. If x ≥ 0, go to step 9.
7. Let s be "-".
8. Let x = –x.
9. If x ≥ 1021, let m = ToString(x) and go to step 20.
10. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as
possible. If there are two such n, pick the larger n.
11. If n = 0, let m be the string "0". Otherwise, let m be the string consisting of the digits of the
decimal representation of n (in order, with no leading zeroes).
12. If f = 0, go to step 20.
13. Let k be the number of characters in m.
14. If k > f, go to step 18.
15. Let z be the string consisting of f+1–k occurrences of the character ‘0’.
16. Let m be the concatenation of strings z and m.
17. Let k = f + 1.
18. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
19. Let m be the concatenation of the three strings a, ".", and b.
20. Return the concatenation of the strings s and m.