给定以下代码:
const value = 1;
Math.sin(2 * Math.PI * value).toFixed(5);
当.toFixed(5)
之前的值为-2.4492935982947064e-16
时,为什么返回"-0.00000"
发布于 2019-06-05 08:47:09
这个数字是用科学记数法表示的。
e-16表示数字左边有16个0。
-2.4492935982947064e-16
是真的
-0.00000000000000024492935982947064
当您运行toFixed(5)时,您最终得到了5位小数,它们都是0。
发布于 2019-06-05 08:46:05
你给-2.4492935982947064e-16
的数字是用科学记数法表示的。这个数字将等同于-2.4492935982947064 × 10^-16
,在扩展它之后,它将确切地为-0.00000000000000024492935982947064
。
发布于 2019-06-05 08:47:39
-2.4492935982947064e-16
是-2.4492935982947064 * Math.pow(10,-16)
,所以小数点后5位不足以看到与0不同的内容
const value = 1;
let result = Math.sin(2 * Math.PI * value);
console.log(result)
console.log(result.toFixed(20))
console.log(result.toFixed(5))
https://stackoverflow.com/questions/56456933
复制相似问题