When adding two float values, I will get something like:
0.3+0.6 = 0.89999999999
I know what's going on. In C# we can use decimal instead, but in Javascript, how to fix it?
Answer
MathUtils
MathUtils = {
roundToPrecision: function(subject, precision) {
return +((+subject).toFixed(precision));
}
};
console.log(MathUtils.roundToPrecision(0.3 + 0.6, 1)) // 0.9;
No comments:
Post a Comment