math.js 503 B

12345678910111213141516171819202122
  1. import { create, all } from 'mathjs'
  2. /**
  3. * 数学运算库math.js的实例.
  4. *
  5. * 用于解决 包括但不限于IEEE754浮点数运算精度 问题.
  6. **/
  7. const mathjs = create(all, { number: 'BigNumber' })
  8. /**
  9. * 转换 {value}, 保留 {precision} 位的小数
  10. **/
  11. export const savePrecision = function (value, precision = 2) {
  12. const temp = Number(value)
  13. return Number(
  14. mathjs.format(typeof temp === 'number' ? temp : 0, (v) =>
  15. v.toFixed(precision)
  16. )
  17. )
  18. }
  19. export default mathjs