I'm trying to get an array of numbers from some min to max by a decimal interval, in this case, 0.02, for example: 1.00, 1.02, 1.04, 1.06
The code below starts to break at the number 1.14:
function getOptions(min, max, interval) {
var options = [];
for (var i = min; i <= max; i += interval) {
options.push(i);
}
return options;
}
var options = getOptions(1.0, 1.4, 0.02);
At this point, options is like:
[
1,
1.02,
...
1.12,
1.1400000000000001,
1.1600000000000001
...
]
What causes this and how can I fix it?
options.push(parseFloat(i.toFixed(2)));, wheretoFixed(n)converts the input tostringkeepingnnumber of decimals.options.push(i.toFixed(2)prior to posting the question, but my loop wasn't working because it wasn't inclusive oni <= max-- usingi.toFixed(2) <= maxis what I needed.