Why does javascript returns so many zeros and not just 0.24 And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
Why does javascript returns so many zeros and not just 0.24 And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum
is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round
or .toFixed
.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));