When I try to calculate 700*0.7 in my code the result gets wrong.
Console shows 489.99999999999994 eventhough the correct result is 490. I really don't understood what the magic is this. Can anyone clarify it? Other numbers are calculated normally.
P.S. U can try this in console chrome. example1
Asked
Active
Viewed 50 times
0
Denny John
- 464
- 7
- 19
Fedor Petrenko
- 33
- 6
-
[Detailed explanation](https://stackoverflow.com/a/21895757/540352) – Laoujin Jun 12 '17 at 09:28
-
Possible duplicate of [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Christopher Moore Jun 12 '17 at 10:27
2 Answers
1
That's some floating numbers issue: http://floating-point-gui.de/
Just round the result, and it should be good. :)
Jonathan Petitcolas
- 3,978
- 4
- 27
- 41
0
Floating point arithmetic is not always 100% accurate. Use the standard Math library functions to be 100% sure of the results when dealing with floating numbers.
Peter
- 9,437
- 14
- 79
- 120