js Number计算精确度

vue使用Number计算丢失精确度,改为使用mathjs BigNumbers计算
下载mathjs文件并引用
计算:

let num1 = 0.1
let num2 = 0.2
console.log(num1 * num2);  // 输出0.020000000000000004
console.log(num1 + num2);  // 输出0.30000000000000004
let add = math.add(math.bignumber(num1), math.bignumber(num2))
console.log(add.toString());  // 输出 0.3
let multiply = math.multiply(math.bignumber(num1), math.bignumber(num2))
console.log(multiply.toString());  // 输出 0.02

你可能感兴趣的:(Web,javascript,vue.js,前端)