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

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);
} 

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

Math.Round(12.5, MidpointRounding.AwayFromZero)

ToEven

AwayFromZero

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

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

posted on 2010-06-20 18:19  steven hu  阅读(7996)  评论(20编辑  收藏