Round() 四捨五入 js銀行家算法(轉)

首先問一下round(0.825,2) 返回的結果,你們猜一猜,算法

首先SQL server 返回的是 0.83ide

js的返回結果 是0.83,code 以下:函數

  var b = 0.825;         alert(Math.round(b * 100) / 100); 其實js中能夠 直接用toFixed函數的,post

  var b = 0.825;         alert(b.toFixed(2));this

這樣也返回0.83spa

 

但是C# 返回的是0.82prototype

       

這裏並非咱們指望的0.83, 爲何了? 其實C#中的Math.Round()並非使用的"四捨五入"法 而是四捨六入五取偶(銀行家算法 Banker's rounding),若須要舍入到的位的後面"小於5"或"大於5"的話,按一般意義的四捨五入處理.若"若須要舍入到的位的後面"等於5",則要看舍入後末位爲偶數仍是奇數.3d

Math.Round(1.25, 1) = 1.2 由於5前面是2,爲偶數,因此把5捨去不進位 Math.Round(1.35, 1) = 1.4 由於5前面是3,爲奇數,因此進位.code

爲了解決這個 問題,微軟提供了其餘的APIserver

Round(Decimal, MidpointRounding)

Round(Double, MidpointRounding)

Round(Decimal, Int32, MidpointRounding)

Round(Double, Int32, MidpointRounding)

網上有人說 這個API 計算小數時有問題, 其實咱們能夠本身實現Round 函數,

public static decimal Round(decimal d, int decimals)
       {
           decimal tenPow = Convert.ToDecimal(Math.Pow(10, decimals));
           decimal scrD = d * tenPow + 0.5m;
           return (Convert.ToDecimal(Math.Floor(Convert.ToDouble(scrD))) / tenPow);
       }

或者以下,

public static decimal Round(decimal d, int decimals)
       {
           d = d + 0.000000000000001m;
           return Decimal.Round(d, decimals);
       }

 

若是咱們如今須要 用js 來實現 銀行家算法,又該怎麼實現了

Number.prototype.round = function (len) {
    var old = this;
    var a1 = Math.pow(10, len) * old;
    a1 = Math.round(a1);
    var oldstr = old.toString()
    var start = oldstr.indexOf(".");
    if (start > 0 && oldstr.split(".")[1].length == len + 1) {
        if (oldstr.substr(start + len + 1, 1) == 5) {
            var flagval = oldstr.substr(start + len, 1) - 0;
            if (flagval % 2 == 0) {
                a1 = a1 - 1;
            }
        }
    }
    var b1 = a1 / Math.pow(10, len);
    return b1;
}
Number.prototype.oldtoFixed = Number.prototype.toFixed;
Number.prototype.toFixed = function (len) {
    var old = this;
    var oldstr = old.toString()
    var start = oldstr.indexOf(".");
    if (len == 2 && start > 0 && oldstr.split(".")[1].length == 3) {
        return this.round(len);
    }
    else {
        return this.oldtoFixed(len);
    }
}
View Code
相關文章
相關標籤/搜索