Round()四舍五入js银行家算法

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

首先问一下round(0.825,2) 返回的结果,大家猜一猜,

首先SQL server 返回的是 0.83

[flash=500,400]http://s1.knowsky.com/20160211/nvzfnmdm2bg32.png[/flash]

js的返回结果 是0.83,code 如下:

var b = 0.825; alert(Math.round(b * 100) / 100); 其实js中可以 直接用toFixed函数的,

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

这样也返回0.83

可是C# 返回的是0.82

[flash=500,400]http://s1.knowsky.com/20160211/u55klgjxbxx33.png[/flash]

这里并不是我们期望的0.83, 为什么了? 其实C#中的Math.Round()并不是使用的"四舍五入"法 而是四舍六入五取偶(银行家算法 Banker's rounding),若需要舍入到的位的后面"小于5"或"大于5"的话,按通常意义的四舍五入处理.若"若需要舍入到的位的后面"等于5",则要看舍入后末位为偶数还是奇数.

Math.Round(1.25, 1) = 1.2 因为5前面是2,为偶数,所以把5舍去不进位 Math.Round(1.35, 1) = 1.4 因为5前面是3,为奇数,所以进位.

为了解决这个 问题,微软提供了其他的API

Round(Decimal, MidpointRounding)

Round(Double, MidpointRounding)

Round(Decimal, Int32, MidpointRounding)

Round(Double, Int32, MidpointRounding)

[flash=500,400]http://s1.knowsky.com/20160211/z0j3y2jcvwx33.png[/flash]

网上有人说 这个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) {varold =this;vara1 = Math.pow(10, len) *old;

a1=Math.round(a1);varoldstr =old.toString()varstart = oldstr.indexOf(".");if(start >0&& oldstr.split(".")[1].length == len +1) {if(oldstr.substr(start + len +1,1) ==5) {varflagval = oldstr.substr(start + len,1) -0;if(flagval %2==0) {

a1= a1 -1;

}

}

}varb1 = a1 / Math.pow(10, len);returnb1;

}

Number.prototype.oldtoFixed=Number.prototype.toFixed;

Number.prototype.toFixed=function (len) {varold =this;varoldstr =old.toString()varstart = oldstr.indexOf(".");if(len ==2&& start >0&& oldstr.split(".")[1].length ==3) {returnthis.round(len);

}else{returnthis.oldtoFixed(len);

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航