美文网首页
vue中使用mathjs

vue中使用mathjs

作者: w_小伍 | 来源:发表于2020-12-18 10:28 被阅读0次

安装

npm install mathjs -S
yarn add mathjs -S

在utils下新建math.js

const $math = require('mathjs')
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())
}

使用
在需要用到的页面引入

import { math } from '@/utils/mathjs.js'

totalPrice() {
      let total = math.multiply(this.buyNum, this.unitPrice)
      if (!total) {
        total = 0
      }
      return total
    }

相关文章

网友评论

      本文标题:vue中使用mathjs

      本文链接:https://www.haomeiwen.com/subject/sodxnktx.html