I've just been experimenting with for loops and noticed this when I changed the increment value to i += 0.1
.
for (i = 0; i < 100; i += 0.1) {
console.log(i);
}
In the console it returns
0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
By the time you reach 100
99.69999999999861
99.7999999999986
99.8999999999986
99.9999999999986
What is the reason behind this?
I imagine this would be frustrating if you are trying to search for a number. Is there a way to round to the nearest 10th? I know how to round with Math.floor, ~~, a | 0
, but only to the nearest whole integer.