0

I used a for loop in JavaScript to count from 0 to 10, incrementing by 0.1 but the console logs weird results. Please see the picture below.

for(let counter = 0; counter < 10; counter += 0.1){
  console.log(counter);
}

Logs contain several fractional digits.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • See [How to deal with floating point number precision in JavaScript?](/q/1458633/4642212). Use `for(let counter = 0; counter < 100; ++counter){ console.log(counter / 10); }` or `Array.from({ length: 100 }, (_, index) => index / 10).forEach((number) => console.log(number));` instead. – Sebastian Simon Oct 20 '22 at 06:44

0 Answers0