相关疑难解决方法(0)

在Javascript中处理浮点精度

y在javascript中有大量的数值.我想通过将它们舍入到最接近的倍数来对它们进行分组x,并将结果转换为字符串.

如何绕过恼人的浮点精度?

例如:

0.2 + 0.4 = 0.6000000000000001
Run Code Online (Sandbox Code Playgroud)

我尝试了两件事:

>>> y = 1.23456789 
>>> x = 0.2 
>>> parseInt(Math.round(Math.floor(y/x))) * x; 
1.2000000000000002
Run Code Online (Sandbox Code Playgroud)

和:

>>> y = 1.23456789 
>>> x = 0.2 
>>> y - (y % x)
1.2000000000000002
Run Code Online (Sandbox Code Playgroud)

javascript floating-point double floating-accuracy numerical-methods

83
推荐指数
3
解决办法
16万
查看次数