我最近做项目,用到了一个四舍六入五成双,查资料发现原来想C#、JAVA这些程序都是用的四舍六入五成双。
后来我跟我一个好朋友说道这个问题,他说这是银行家算法。现在小学数学课本上都是这个了。听得我狂汗,落伍拉?
具体如下:
四舍六入五考虑,即当尾数≤4时舍去,尾数为6时进位。当尾数4舍为5时,则应是末位数是奇数还是偶数,5前为偶数应将5舍去,5前为奇数应将5进位。
我做了个程序测试如下:
我做了个程序测试如下:
decimal aa=2.45M;
decimal bb=2.35M;
decimal cc=2.44M;
decimal dd=2.46M;
decimal ee=2.350M;
decimal ff=2.351M;
decimal gg=2.450M;
decimal hh=2.451M;
decimal ii=2.456M;
Response.Write("<br><br><br>"+Math.Round(aa,1)+"<br>"+Math.Round(bb,1)+"<br>"+Math.Round(cc,1)+"<br>"+Math.Round(dd,1)+"<br>"+Math.Round(ee,1)+"<br>"+Math.Round(ff,1)+"<br>"+Math.Round(gg,1)+"<br>"+Math.Round(hh,1)+"<br>"+Math.Round(ii,1));
decimal bb=2.35M;
decimal cc=2.44M;
decimal dd=2.46M;
decimal ee=2.350M;
decimal ff=2.351M;
decimal gg=2.450M;
decimal hh=2.451M;
decimal ii=2.456M;
Response.Write("<br><br><br>"+Math.Round(aa,1)+"<br>"+Math.Round(bb,1)+"<br>"+Math.Round(cc,1)+"<br>"+Math.Round(dd,1)+"<br>"+Math.Round(ee,1)+"<br>"+Math.Round(ff,1)+"<br>"+Math.Round(gg,1)+"<br>"+Math.Round(hh,1)+"<br>"+Math.Round(ii,1));
输出结果为:
2.4
2.4
2.4
2.5
2.4
2.4
2.4
2.5
2.5
2.4
2.4
2.5
2.4
2.4
2.4
2.5
2.5