2011-03-02 43 views
0

如何在javascript中绕浮点错误?如何在javascript中绕浮点错误

+1

使用toFixed()方法,http://www.w3schools.com/jsref/jsref_tofixed.asp – 2011-03-02 13:34:17

+0

...所以错误是圆的?对不起,无法抗拒。 ;-) – deceze 2011-03-02 13:46:15

回答

3

使用toFixed圆它,然后将其转换回数字

var num = 1.00000000000000002341; 
alert(+num.toFixed(10)); 

DEMO

0

为了完善对0获得32位有符号整数,使用x | 0

还有Math.round函数向最接近的整数舍入。

0

您可以使用Math.round(number * 1E6)/1E6四舍五入到小数点后6位。