JS-- 浮點數運算處理

    一. 問題描述javascript

     最近在作一個項目,頁面上會存在一些JS浮點數的運算,發現JS浮點數運算存在一些bug.譬如:前端

0.1+0.2 == 0.30000000000000004

0.1 + 0.7 == 0.7999999999999999

7*0.8 == 5.6000000000000005

5.6/7 == 0.7999999999999999

 

   二.解決方案    java

   JS運算後都會有很小的偏差. 不像.Net或者Java那樣準確. 主要是JS重點不在運算上面,但是有時候項目必定要用到.想了一下大概有兩種解決方案this

     A 方案一: prototype

    運算結果保留2-3位小數位數. 前端界面通常用到的運算比較少。精度要求不會過高。 因此取2位小數位便可。blog

     B. 方案二:ip

           將小數位數轉換爲整數運算. 譬如:ci

0.1+0.2 =》 (1+2)/10 == 0.3

0.1 + 0.7 =》 (1+7)/10 == 0.8

7*0.8 == (7*8)/10 == 5.6

5.6/7 == (56/7)/10 == 0.1

       爲了方便調用. 因此咱們能夠提取一個公共的方法出來.譬以下面的JSMath庫,JSMath重寫了加減乘除. 會先將參數轉換爲整數再運算JSMath(參數1).操做(參數2)get

       參數1和參數2分別就是運算的第一個Number和第二個Number. 計算後經過Value屬性獲取值.it

(function() {

    var JSMath = function() {
        return this;
    }

    JSMath.prototype.from = function(value) {

        // 支持JSMath參數傳遞主要是用於嵌套的調用
        if ((typeof(value) == 'object') && (value.value != undefined)) {
            this.value = value.value;
        } else {
            this.value = value;
        }
        return this;
    }

  // 加法
    JSMath.prototype.add = function(value) {
        var thisValueString = this.value.toString();
        var valueString = value.toString();
        var timesCount1 = 0;
        var timesCount2 = 0;
        if (thisValueString.indexOf('.') > 0) {
            timesCount1 = thisValueString.split('.')[1].length;
        }
        if (valueString.indexOf('.') > 0) {
            timesCount2 = valueString.split('.')[1].length;
        }
        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
        this.value = ((Math.pow(10, maxtimeCount) * this.value + Math.pow(10, maxtimeCount) * value)) / Math.pow(10, maxtimeCount);
        return this;
    }
   
 // 減法
    JSMath.prototype.sub = function(value) {
        var thisValueString = this.value.toString();
        var valueString = value.toString();
        var timesCount1 = 0;
        var timesCount2 = 0;
        if (thisValueString.indexOf('.') > 0) {
            timesCount1 = thisValueString.split('.')[1].length;
        }
        if (valueString.indexOf('.') > 0) {
            timesCount2 = valueString.split('.')[1].length;
        }
        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
        this.value = ((Math.pow(10, maxtimeCount) * this.value - Math.pow(10, maxtimeCount) * value)) / Math.pow(10, maxtimeCount);
        return this;
    }

    // 除法  
    JSMath.prototype.div = function(value) {
        var thisValueString = this.value.toString();
        var valueString = value.toString();
        var timesCount1 = 0;
        var timesCount2 = 0;
        if (thisValueString.indexOf('.') > 0) {
            timesCount1 = thisValueString.split('.')[1].length;
        }
        if (valueString.indexOf('.') > 0) {
            timesCount2 = valueString.split('.')[1].length;
        }
        var maxtimeCount = timesCount1 > timesCount2 ? timesCount1 : timesCount2;
        this.value = ((Math.pow(10, maxtimeCount) * this.value) / (Math.pow(10, maxtimeCount) * value));
        return this;
    }

  // 乘法
    JSMath.prototype.times = function(value) {

        var thisValueString = this.value.toString();
        var valueString = value.toString();
        var timesCount1 = 0;
        var timesCount2 = 0;
        if (thisValueString.indexOf('.') > 0) {
            timesCount1 = thisValueString.split('.')[1].length;
        }
        if (valueString.indexOf('.') > 0) {
            timesCount2 = valueString.split('.')[1].length;
        }
        var maxtimeCount =  timesCount1 + timesCount2;
        this.value = (Math.pow(10, maxtimeCount) * this.value * Math.pow(10, maxtimeCount) * value) / Math.pow(10, maxtimeCount * 2);
        return this;
    }

    if (window.JSMath == undefined) {
        window.JSMath = function(value) {
            var result = new JSMath();
            result.from(value);
            return result;
        }
    }
})()
 

           B1.基本運算

0.1+0.2
=> JSMath(0.1).add(0.2).value == 0.3

7+0.8
=> JSMath(7).times(0.8).value == 5.6

5.6/7
=> JSMath(5.6).div(7).value = 0.8

        B2.多目運算

0.05 + 0.05 + 0.2
=> JSMath(JSMath(0.05).add(0.05)).add(0.2).value == 0.3

(5+0.6)/7
=> JSMath(JSMath(5).add(0.6)).div(7).value == 0.8

  三.小總結

    上面本身本身暫時知道的一些解決方案.不太清楚是否有開源的更可靠的三方庫來解決這個問題,若是有的話,但願博友推薦一下。

            貼一下Stockoverflow 裏面看到的一些解決方案:

            http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript

           http://stackoverflow.com/questions/3556789/javascript-math-error-inexact-floats

相關文章
相關標籤/搜索