看到一个帖子《ASP.net中c#里有没有四舍五入函数? 3.4进行四舍五入的计算在c#里如何实现?》
http://community.csdn.net/Expert/TopicView3.asp?id=5371508关于四舍五入的讨论和对 Round 函数的误解网上有很多,我在CSDN的第一帖就来说说这个简单问题,欢迎大家拍砖,呵呵C# 中没有四舍五入函数,事实上我知道的程序语言都没有四舍五入函数,因为四舍五入算法不科学,国际通行的是 Banker 舍入法Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的Math.Round 方法默认的也是 Banker 舍入法在 .NET 2.0 中 Math.Round 方法有几个重载方法
Math.Round(Decimal, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Double, Int32, MidpointRounding)将小数值舍入到指定精度。MidpointRounding 参数,指定当一个值正好处于另两个数中间时如何舍入这个值该参数是个 MidpointRounding 枚举此枚举有两个成员,MSDN 中的说明是:
AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。注意!这里关于 MidpointRounding.AwayFromZero 的说明是错误的!实际舍入为两个值中绝对值较大的值。不过 MSDN 中的例子是正确的,英文描述原文是 it is rounded toward the nearest number that is away from zero.所以,要实现四舍五入函数,对于正数,可以加一个 MidpointRounding.AwayFromZero 参数指定当一个数字是其他两个数字的中间值时其舍入为两个值中绝对值较大的值,例:Math.Round(3.45, 2, MidpointRounding.AwayFromZero)不过对于负数上面的方法就又不对了因此需要自己写个函数来处理double ChinaRound(double value, int decimals)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
}
http://community.csdn.net/Expert/TopicView3.asp?id=5371508关于四舍五入的讨论和对 Round 函数的误解网上有很多,我在CSDN的第一帖就来说说这个简单问题,欢迎大家拍砖,呵呵C# 中没有四舍五入函数,事实上我知道的程序语言都没有四舍五入函数,因为四舍五入算法不科学,国际通行的是 Banker 舍入法Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的Math.Round 方法默认的也是 Banker 舍入法在 .NET 2.0 中 Math.Round 方法有几个重载方法
Math.Round(Decimal, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Double, Int32, MidpointRounding)将小数值舍入到指定精度。MidpointRounding 参数,指定当一个值正好处于另两个数中间时如何舍入这个值该参数是个 MidpointRounding 枚举此枚举有两个成员,MSDN 中的说明是:
AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。注意!这里关于 MidpointRounding.AwayFromZero 的说明是错误的!实际舍入为两个值中绝对值较大的值。不过 MSDN 中的例子是正确的,英文描述原文是 it is rounded toward the nearest number that is away from zero.所以,要实现四舍五入函数,对于正数,可以加一个 MidpointRounding.AwayFromZero 参数指定当一个数字是其他两个数字的中间值时其舍入为两个值中绝对值较大的值,例:Math.Round(3.45, 2, MidpointRounding.AwayFromZero)不过对于负数上面的方法就又不对了因此需要自己写个函数来处理double ChinaRound(double value, int decimals)
{
if (value < 0)
{
return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
}
else
{
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
}
说的再简单些再通俗些就好了.
---------------------
这个...我觉得够简单了吧,呵呵to zswang(伴水清清)
x = int(x + 0.5)
-------------------
没错,我也常用这种方法,还有很多方法可以实现如 WeiWY(大海) 说的
double.Parse(d.ToString("0.0"));我只所以用个麻烦的例子,目的是想解释下 Math.Round 的用法
return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
x=-0.5 你就不知道减0.5吗 x = int(x - 0.5)你这么死板 怎么做开发啊
上面的也不是绝对,试试下面的
------------------------------------
Math.Round(2.44445, 4, MidpointRounding.AwayFromZero);
------
结果:2.4444
还真是这样,我对MATH.ROUND有点糊涂了……