js數值計算的精度問題


//加法函數  
function accAdd(arg1, arg2) {  
    var r1, r2, m;  
    try {  
        r1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r1 = 0;  
    }  
    try {  
        r2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r2 = 0;  
    }  
    m = Math.pow(10, Math.max(r1, r2));  
    return (arg1 * m + arg2 * m) / m;  
}   
//給Number類型增加一個add方法,,使用時直接用 .add 即可完成計算。   
Number.prototype.add = function (arg) {  
    return accAdd(arg, this);  
};  
  
//減法函數  
function Subtr(arg1, arg2) {  
    var r1, r2, m, n;  
    try {  
        r1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r1 = 0;  
    }  
    try {  
        r2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r2 = 0;  
    }  
    m = Math.pow(10, Math.max(r1, r2));  
     //last modify by deeka  
     //動態控制精度長度  
    n = (r1 >= r2) ? r1 : r2;  
    return ((arg1 * m - arg2 * m) / m).toFixed(n);  
}  
  
//給Number類型增加一個add方法,,使用時直接用 .sub 即可完成計算。   
Number.prototype.sub = function (arg) {  
    return Subtr(this, arg);  
};  
  
//乘法函數  
function accMul(arg1, arg2) {  
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();  
    try {  
        m += s1.split(".")[1].length;  
    }  
    catch (e) {  
    }  
    try {  
        m += s2.split(".")[1].length;  
    }  
    catch (e) {  
    }  
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);  
}   
//給Number類型增加一個mul方法,使用時直接用 .mul 即可完成計算。   
Number.prototype.mul = function (arg) {  
    return accMul(arg, this);  
};   
  
//除法函數  
function accDiv(arg1, arg2) {  
    var t1 = 0, t2 = 0, r1, r2;  
    try {  
        t1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
    }  
    try {  
        t2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
    }  
    with (Math) {  
        r1 = Number(arg1.toString().replace(".", ""));  
        r2 = Number(arg2.toString().replace(".", ""));  
        return (r1 / r2) * pow(10, t2 - t1);  
    }  
}   
//給Number類型增加一個div方法,,使用時直接用 .div 即可完成計算。   
Number.prototype.div = function (arg) {  
    return accDiv(this, arg);  
};   

 除法精度:

function exc(val, valTwo = 100) {
      const strVal = val.toString()
      const strValTwo = valTwo.toString()
      const index = strVal.indexOf('.')
      const indexTwo = strValTwo.indexOf('.')
      const num = [0, 0]
      if (index > -1) {
        num[0] = strVal.length - 1 - index
      }
      if (indexTwo > -1) {
        num[1] = strValTwo.length - 1 - index
      }
      return Math.round(val * Math.pow(10, num[0])) / (Math.round((valTwo * Math.pow(10, num[1]))) * Math.pow(10, num[0] - num[1]))
    }

 使用:exc(0.5, 0.2) => 2.5, 只允許傳入兩個參數。如果計算出現無窮數請后期根據需要修改最后代碼進行取舍。

乘法精度:

function ride(...val) {
      const strVal = val[0].toString()
      const strValTwo = val[1].toString()
      const index = strVal.indexOf('.')
      const indexTwo = strValTwo.indexOf('.')
      const num = [0, 0]
      if (index > -1) {
        num[0] = strVal.length - 1 - index
      }
      if (indexTwo > -1) {
        num[1] = strValTwo.length - 1 - index
      }
      return Math.round((val[0] * Math.pow(10, num[0])) * (val[1] * Math.pow(10, num[1]))) / Math.pow(10, num[0] + num[1])
    }
 使用:ride(0.5, 0.6) => 3, 只允許傳入兩個參數。%計算可以這樣ride(0.5, 100) => 50。

減法精度:

function sub(...val) {
      let max = 0
      let count = val[0] | 0
      for (let i = 0; i < val.length; i++) {
        const strVal = val[i].toString()
        const index = strVal.indexOf('.')
        let num = 0
        if (index > -1) {
          num = strVal.length - 1 - index
          max = num > max ? num : max
        }
      }
      for (let i = 0; i < val.length; i++) {
        count -= Math.round(val[i] * Math.pow(10, max))
      }
      return count / Math.pow(10, max)
    }
 使用:sub(1, 0.2, 0.3, 0.4) => 0.1。相當於1 - 0.2 -0.3 -0.4;可以傳多個參數,使用首位減后面的所有參數。

加法精度:

function add(...val) {
      let max = 0
      let count = 0
      for (let i = 0; i < val.length; i++) {
        const strVal = val[i].toString()
        const index = strVal.indexOf('.')
        let num = 0
        if (index > -1) {
          num = strVal.length - 1 - index
          max = num > max ? num : max
        }
      }
      for (let i = 0; i < val.length; i++) {
        count += Math.round(val[i] * Math.pow(10, max))
      }
      return count / Math.pow(10, max)
    }
 使用:add(0.1, 0.2, 0.3, 0.4) => 1。可以傳多個參數進行相加。

 

 

https://blog.csdn.net/github_38186390/article/details/84924741


免責聲明!

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



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