下载依赖
npm install mathjs
封装js
let $math = require('mathjs');
// import $math from 'mathjs'
const math = {
//加法
add() {
return comp('add', arguments)
},
//减法
subtract() {
return comp('subtract', arguments)
},
// 乘法
multiply() {
return comp('multiply', arguments)
},
// 除法
divide() {
return comp('divide', arguments)
},
}
function comp(_func, args) {
let t = $math.chain($math.bignumber(args[0]));
for (let i=1; i<args.length; i++) {
t = t[_func]($math.bignumber(args[i]))
}
// 防止超过6位使用科学计数法
return parseFloat(t.done())
}
export default math;
页面引用js
import math from '@/utils/moth';
使用
//加
math.add(1.00,0.24)
//减
math.subtract(1.00,0.24)
//乘
math.multiply(1.00,0.24)
//除
math.divide(1.00,0.24)