1.安装
cnpm install mathjs --save
2.新建math.js文件
const $math = require('mathjs')
PS:最新版本的引入方法可能不一样
export 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())
}
3.页面引入
import { math } from '@/utils/math.js'
除法
this.customerPrice = math.subtract(this.interestRate, this.oldRate)
乘法
this.customerPrice = math.multiply(this.customerPrice, 100)
加法
this.customerPrice = math.add(this.customerPrice, 100)
减法
this.customerPrice = math.divide(this.customerPrice, 100)
取决于math.js文件里面的命名,PS:中间是逗号