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.
精彩评论