Strawberry
Strawberry

Reputation: 67888

JavaScript: Can I add decimals?

for( count = 0.01; count <= 0.20; count + 0.01 ) 

Is this valid? Because it seems as soon as I changed it to this from count++, my firefox crashed.

Upvotes: 0

Views: 830

Answers (2)

Joeri Sebrechts
Joeri Sebrechts

Reputation: 11146

If you use this code, you'll get values like

  • 0.060000000000000005
  • 0.11999999999999998

If you really want a predictable count, keep the loop integer, and rescale down to the fractional number you need:

for( count = 1; count <= 20; count++ ) console.log(count/100)

This produces values like 0.06 and 0.12, like you would expect.

Upvotes: 2

rahul
rahul

Reputation: 187070

for( count = 0.01; count <= 0.20; count += 0.01 ) 

You were missing an = operator in the last section of the for loop. Otherwise it would be an infinite loop.

Upvotes: 3

Related Questions