Thursday, March 22, 2018

math - How to fix 0.3+0.6=0.89999999999 in Javascript?




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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...