문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top