Skip to content

Increment number by 0.01 in javascript using a loop

Recently, I took over a project where I had to replace numbers starting from 1.xx and ending up at 10 on a page. Initially, the code was:

but the issue with the code was that it was generating number like:

1.1300000000000001
1.1400000000000001
1.1500000000000001
1.1600000000000001
1.1700000000000002
1.1800000000000002
1.1900000000000002
1.2000000000000002
1.2100000000000002
1.2200000000000002

The issue was with the inaccuracy of floating point numbers. To fix this issue I used:

Published inJavaScript

Be First to Comment

Leave a Reply

%d bloggers like this: