开发者

How to round floating point error in javascript

开发者 https://www.devze.com 2023-02-14 20:59 出处:网络
How to round floating poi开发者_如何学JAVAnt error in javascript?use toFixed to round it then convert it back to a number

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

精彩评论

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