JS精度問題,解決方案 math.js


 

 

 JS精度問題    Vue中使用 解決方案 math.js 

 npm install mathjs

 

 

import { create, all } from 'mathjs'
 
const config = { 
  number: 'BigNumber',
  precision: 20
}
const math = create(all, config);
export default {
  methods: {
    
     // 除
    numberExcept: function (arg1, arg2) {
      return math.divide(arg1, arg2);
    },
    //乘
    numberRide: function (arg1, arg2) {      
      return math.multiply(arg1, arg2);
    },
    //加
    numberAdd:function (arg1,arg2) {
    return math.add(arg1, arg2);
    }
    //減
    numberSub:function (arg1,arg2) {
    return math.add(arg1, -arg2);
    }
  }
 
}
 
js/引入,初始化后 ,,金額精度處理 
function getMath(number) {
    const math = window.math.create(window.math.all, {
        number: 'BigNumber',
        precision: 14
    });
    return Number(math.format(number, 14))
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM