# JS 中运算精读问题
为什么 0.1 + 0.2 !== 0.3 ?看看这篇文章
下面是简单的处理下这个问题,主要用的 toFixed
这个 API;
const count = (value, precision = 10) => {
if (isNaN(value)) return value;
return Number(value.toFixed(precision));
}
console.log(count(0.1 + 0.2));
console.log(count(1 - 0.9));
console.log(count(1 / 3));
console.log(count(0.585 + 0.75));
处理数学计算最好还是引用数学计算库:
mathjs:
GitHub - josdejong/mathjs: An extensive math library for JavaScript and Node.js
Number.js:
GitHub - numbers/numbers.js: Advanced Mathematics Library for Node.js and JavaScript
Numeric
GitHub - sloisel/numeric: Numerical analysis in Javascript