Skip to main content
Known Participant
November 10, 2015
Answered

Adding .1 with a for loop?

  • November 10, 2015
  • 1 reply
  • 448 views

I attempted to add increment of .1 to a value using a for loop, but the for loop did not add to the value in precise increments.

So, instead of getting  .1,.2,.3,.4, etc, I got the following when I traced the loop:

0

0.1

0.2

0.30000000000000004

0.4

0.5

0.6

0.7

0.7999999999999999

0.8999999999999999

0.9999999999999999

This throws my code off completely because it causes the loop to run one unnecessary time and the value returned causes other functions to work improperly. Please advise on how to fix this.  I notice that this is not a problem once you get to .25.

This topic has been closed for replies.
Correct answer nezarov

use .toFixed(1); after the value

1 reply

nezarovCorrect answer
Inspiring
November 10, 2015

use .toFixed(1); after the value

withertonAuthor
Known Participant
November 10, 2015

Thanks for prompt response.

Inspiring
November 10, 2015

You're welcome.