import * as math from "mathjs"
0.1+0.2
math.format(math.chain(math.bignumber(0.1)).add(math.bignumber(0.2)).done());
0.2-0.1
math.format(math.chain(math.bignumber(0.2)).subtract(math.bignumber(0.1)).done());
0.1*0.2
math.format(math.chain(math.bignumber(0.1)).multiply(math.bignumber(0.2)).done());
0.1/0.2
math.format(math.chain(math.bignumber(0.1)).divide(math.bignumber(0.2)).done());
"mathjs": "^10.6.4",
import { create, all } from 'mathjs'
const math = create(all, { number: 'BigNumber', precision: 20 })
const { format, bignumber, add, subtract, multiply, divide } = math;
// 计算
export function calculation(type, ...rest) {
// add加 subtract减 multiply乘 devide除
let opear = add;
switch (type) {
case '+': // 加
opear = add;
break;
case '-': // 减
opear = subtract;
break;
case '*': // 乘
opear = multiply;
break;
case '/': // 除
opear = divide;
break;
}
return rest.reduce((total, item) => +format(opear(bignumber(total), bignumber(item))));
}
使用
calculation('+',0.1,0.2)
网友评论