I know I must be missing something here, I am trying to round an arbitrary amount of numbers to two decimals without success.
var sumOne = 0;
var sumTwo = 0;
[12.993, 12.99, 12.99].forEach(function(number){
sumOne += roundOne(number * 0.1);
sumTwo += roundTwo(number * 0.1);
})
function roundOne(n){
return (Math.round((n *100))/100);
}
function roundTwo(n){
return +(n.toFixed(2));
}
console.log("sumOne is ", sumOne, " sumTwo is ", sumTwo);
https://jsfiddle.net/m90ecdh9/1/
In this jsfiddle or code above I end up with the following output
sumOne is 3.9000000000000004 sumTwo is 3.9000000000000004
Why do both these methods of rounding fail to result in a sum with only two decimal places?
Strangely, if I attempt this same code with only two numbers in the array, my result is as expected with sums that have only two decimal places by both methods.