您的位置:首页 > 其它

Math.Round 之关于 不同的四舍五入

2010-06-20 18:19 501 查看
之前有一直被误导,以为就是简单的四舍五入。

写这篇文章之前也是搜索一下,博客园中也是很多写过关于的文章。

本以为是很简单的就过去了,却发现了一个很不同现象。下面等我慢慢道来。

Math.Round有好多个重载,如下:

[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern double Round(double a);

public static double Round(double value, int digits)
{
if ((digits < 0) || (digits > maxRoundingDigits))
throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits"));
return InternalRound(value, digits, MidpointRounding.ToEven);
}

public static double Round(double value, MidpointRounding mode) {
return Round(value, 0, mode);
}

public static double Round(double value, int digits, MidpointRounding mode) {
if ((digits < 0) || (digits > maxRoundingDigits))
throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits"));
if (mode < MidpointRounding.ToEven || mode > MidpointRounding.AwayFromZero) {
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidEnumValue", mode, "MidpointRounding"), "mode");
}
return InternalRound(value, digits, mode);
}

public static Decimal Round(Decimal d) {
return Decimal.Round(d,0);
}

public static Decimal Round(Decimal d, int decimals) {
return Decimal.Round(d,decimals);
}

public static Decimal Round(Decimal d, MidpointRounding mode) {
return Decimal.Round(d, 0, mode);
}

public static Decimal Round(Decimal d, int decimals, MidpointRounding mode) {
return Decimal.Round(d, decimals, mode);
}


由于翻了源代码,就直接贴了源代码。

这里是有两个版本的,double和decimal版本的,注意这两个版本的代码最终调用的方法可是不一样的哦,后面的现象原因就出在这里

 

先来简单的例子:

Math.Round(12.5)


返回的是12,这个在搜索出来的资料是没有问题,可以解释为Banker 's  rounding(银行家舍入)算法,即四舍六入五取偶,是IEEE规定的舍入标准。

当然我们需要四舍五入的话,需要设置 MidpointRounding .

Math.Round(12.5, MidpointRounding.AwayFromZero)


则返回13。

来看一下MidpointRounding的枚举介绍(MSDN)。

ToEven

当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。

AwayFromZero

当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。

在默认情况下都是使用ToEven,在上面的代码中可以发现。

所以一开始以为只要使用AwayFromZero就可以完成四舍五入了。

 

下面再来一个例子:

Math.Round(0.145, 2, MidpointRounding.AwayFromZero)


Math.Round(0.145, 2, MidpointRounding.ToEven)


我想很多同学都会觉得第一个返回0.15,第二个返回0.14.

问题来了,结果不是这样的哦。正确结果两个都是0.14。 哇靠,AwayFromZero没起作用,没给我四舍五入!

不知道还有没有其他存在这样的例子,看样子AwayFromZero也不可靠啊。

其实这里是调用double的方法。

下面来试一下decimal的方法:

Math.Round((decimal)0.145, 2, MidpointRounding.AwayFromZero)


Math.Round((decimal)0.145, 2, MidpointRounding.ToEven)


这回结果与上面的版本不一样哦,得到了我想要的0.15和0.14。

 

不知道是不是.Net Framework的Math.Round的double版本一个bug呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: