Problem
I was trying to build out a list of heights in the console by meteres starting from 1.20m and ending up at 2.50m.
I used this code:
var heights = [];
for ( var i=1.20, l=2.5; i<l; i+=0.01 ){
heights.push(i);
}
heights = heights.join('\n');
If I console.log( heights )
I get:
1.2
1.21
1.22
1.23
...
But then at 1.37 I start getting:
1.37
1.3800000000000001
1.3900000000000001
1.4000000000000001
1.4100000000000001
1.4200000000000002
1.4300000000000002
Questions
- What's going on?
- How do I fix it?
Demo
var heights = [];
for ( var i=1.20, l=2.5; i<l; i+=0.01 ){
heights.push(i);
}
var heights = heights.join('\n');
document.querySelector('#output').innerText = heights;
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="output"></div>
</body>
</html>