Console.log(sum of a bunch of num) return weird number

I’m currently doing the last project of javascript algorithm.

Here is my code

console.log(60 + 20 + 15 + 1 + 0.50 + 0.20 + 0.04)

the output is 96.74000000000001

the problem seems to be the last number “0.04” if I change it to “0.03” or “0.05” it works fine.

You have discovered one of the problems with floating point values. Computers can’t exactly store decimal numbers, so the arithmetic will have rounding errors.

I recommend using a whole number of cents instead. No decimals means no rounding.

1 Like

a computer can’t store precisely decimal numbers
You can get some more explanation in this video:

2 Likes

that was very quick, I have also seen ieahleen’s reply. I will check the video out, thanks you so much.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.