Why, I get result with many decimal of sum value (13.98 + 6.58)?


#1

I have example:

var totalInterest = 13.98 + 6.58;
alert(totalInterest); // 20.560000000000002

Why i get this result (should be 20.56)


#2

What he said (below)


#3

http://www.w3schools.com/js/js_numbers.asp


#4

The 0000000000002 is still a bit of a mystery, ain’t it?


#5

To add to the above, if you want to display results to the end-user with 2 decimal places, you can use toFixed():

alert(totalInterest.toFixed(2));

However, this should not be used for storing or representing data internally (toFixed() returns string data).


#6

This is the result of floating point binary math. The short answer is there are different ways a computer can represent a decimal number in binary with varying levels of accuracy and limitations. Floating point is one of those ways and what we’re seeing here is that the number 20.56 can not be perfectly represented as a binary decimal, but most closely represented as 20.560000000000002 (which we can then truncate to 20.56 since no one will miss the remainder).

See this answer for some more info and links to other resources: http://stackoverflow.com/questions/588004/is-floating-point-math-broken

For fun, try this in your browser console:

(1398 + 658) / 100
// gives 20.56

Also consider as another example, that 1/3 can not be perfectly represented (rationally) in base-10 decimal notation. Same principle.


#7

So It mean that, if I have sum, … or any operator of calculation I must to fixed it before using.
Oh . . .


#8

Only if you’re using decimal numbers. If you’re using integers you should be fine. But if you don’t know if your input will be a decimal or integer, then yeah.


#9

User can input both (decimal/integer), but I calculate this value before insert to db.
Could you recommend, how to format the value before insert?


#10

Don’t format numbers stored in the DB: formatted numbers are strings not numbers. Format only on presentation.


#11

So I should be insert with the original amount (20.560000000000002).
Don’t format it.


#12

I’d say format it since the 0000000000000000000000000002 part is not correct anyway. It will do nothing good. Ever.


#13

The point of not formatting it is so it remains the correct type, number, as using toFixed() returns a string.


#14

Just to continue on this, you can read about .toFixed() here. If your goal is to keep the number as a number so you can do math on it when you pull it from the db then you should not format it. If you format it then any time you fetch it from the db you are going to convert it back to a number using Number() or parseFloat() which would probably get annoying really quickly.

As an aside, this has nothing to do with meteor. This is a problem with javascript.


#15

Yes, turning it to a string makes no sense, I was thinking more i the terms like:

function round(value, decimals) {
    return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
var totalInterest = 13.98 + 6.58;
alert(round(totalInterest, 2));

#16

You can try 0.1+0.5+0.3 in browser console and will get same result. Its common behavior for C based languages