Floating point is notoriously tricky. Basically it boils down to that there are an infinite amount of values between two real numbers, so it is impossible to represent them correctly in a computer.
If you want to print the number i suggest:
total.toFixed();
Which will always give you three decimal places. And when you want to check if two floats are the same you need to do something like this:
function nearlyEqual(a, b, epsilon) {
var absA = Math.abs(a);
var absB = Math.abs(b);
var diff = Math.abs(a - b);
var minNormal = 1.1754943508222875e-38;
if (a == b) { // shortcut, handles infinities
return true;
} else if (a == 0 || b == 0 || diff < minNormal) {
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < (epsilon * minNormal);
} else { // use relative error
return diff / (absA + absB) < epsilon;
}
}
nearlyEqual(0.1+0.2, 0.3, 0.0001);
As is suggested here How should I do floating point comparison?