Question

I have multiple calculations which have troubles because of floating math problem in js. I'm trying to fix it by multiplying all parts of equation by 1000 and then dividing the result by 1000.

var a = 16924.83,
    b = 16835.61;

var c = a-b; //outputs 89.22000000000116
var d = (1000*a-1000*b)/1000;  //outputs 89.22 and seems to be working correct

Is it ok to do correction of floating math problem this way?

I did read Is floating point math broken?

Was it helpful?

Solution

You should round after multiplying:

var d = (Math.round(1000*a)-Math.round(1000*b))/1000;

Just multiplying may not solve the problem. When you assign

var a = 16924.83;

it might internally represent this as something like 16924.8300001, so when you multiply by 1000 you get 16924830.001. You need to round to get rid of that fraction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top