开发者

How to round floating point error in javascript

How to round floating poi开发者_如何学JAVAnt error in javascript?


use toFixed to round it then convert it back to a number

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

DEMO


To round towards 0 to get a 32 bit signed integer, use x | 0.

There's also the Math.round function which rounds towards the closest integer.


You could use Math.round(number * 1E6) / 1E6 to round up to 6 decimal places.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜