Sometimes, we want to increment number by 0.01 in JavaScript using a loop.
In this article, we’ll look at how to increment number by 0.01 in JavaScript using a loop.
How to increment number by 0.01 in JavaScript using a loop?
To increment number by 0.01 in JavaScript using a loop, we can use the JavaScript number’s toFixed
method.
For instance, we write:
const heights = [];
for (let i = 1.20, l = 2.5; i < l; i += 0.01) {
heights.push(i.toFixed(2));
}
console.log(heights)
We add a for loop that increments i
by 0.01 with each iteration.
Then we call i.toFixed
with 2 to round i
to 2 decimal places and convert it to a string.
Then we call height.push
to append the string to the array.
Conclusion
To increment number by 0.01 in JavaScript using a loop, we can use the JavaScript number’s toFixed
method.