在c#中,Math.Round(2.5)的结果是2。

应该是3,不是吗?为什么在c#中它是2 ?


当前回答

Silverlight不支持midpointrsurround选项。 下面是Silverlight的一个扩展方法,它添加了midpointrsurround枚举:

public enum MidpointRounding
{
    ToEven,
    AwayFromZero
}

public static class DecimalExtensions
{
    public static decimal Round(this decimal d, MidpointRounding mode)
    {
        return d.Round(0, mode);
    }

    /// <summary>
    /// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding
    /// </summary>
    /// <param name="d">A Decimal number to be rounded.</param>
    /// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param>
    /// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns>
    public static decimal Round(this decimal d, int decimals, MidpointRounding mode)
    {
        if ( mode == MidpointRounding.ToEven )
        {
            return decimal.Round(d, decimals);
        }
        else
        {
            decimal factor = Convert.ToDecimal(Math.Pow(10, decimals));
            int sign = Math.Sign(d);
            return Decimal.Truncate(d * factor + 0.5m * sign) / factor;
        }
    }
}

来源:http://anderly.com/2009/08/08/silverlight-midpoint-rounding-solution/

其他回答

您应该检查MSDN中的Math。轮:

这种方法的行为遵循IEEE标准754,第4节。这种四舍五入有时被称为最接近四舍五入,或银行家四舍五入。

您可以指定Math的行为。使用重载进行四舍五入:

Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // gives 3

Math.Round(2.5, 0, MidpointRounding.ToEven); // gives 2

Silverlight不支持midpointrsurround选项。 下面是Silverlight的一个扩展方法,它添加了midpointrsurround枚举:

public enum MidpointRounding
{
    ToEven,
    AwayFromZero
}

public static class DecimalExtensions
{
    public static decimal Round(this decimal d, MidpointRounding mode)
    {
        return d.Round(0, mode);
    }

    /// <summary>
    /// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding
    /// </summary>
    /// <param name="d">A Decimal number to be rounded.</param>
    /// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param>
    /// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns>
    public static decimal Round(this decimal d, int decimals, MidpointRounding mode)
    {
        if ( mode == MidpointRounding.ToEven )
        {
            return decimal.Round(d, decimals);
        }
        else
        {
            decimal factor = Convert.ToDecimal(Math.Pow(10, decimals));
            int sign = Math.Sign(d);
            return Decimal.Truncate(d * factor + 0.5m * sign) / factor;
        }
    }
}

来源:http://anderly.com/2009/08/08/silverlight-midpoint-rounding-solution/

来自MSDN的数学。Round(double a)返回:

最接近a的整数 a的分数分量是一半 两个整数之间,其中一个是 偶数和另一个奇数,然后是偶数 返回Number。

... 因此,在2和3中间的2.5被四舍五入为偶数(2)。这被称为银行家四舍五入(或四舍五入为偶数),是一种常用的四舍五入标准。

同一篇MSDN文章:

此方法的行为如下 IEEE标准754,第4节。这 四舍五入有时被称为 四舍五入到最接近的,或银行家的 舍入。它最小化舍入误差 这是持续舍入的结果 一个单一的中点值 方向。

您可以通过调用Math的重载来指定不同的舍入行为。回合采取midpointround模式。

以下是我必须解决的方法:

Public Function Round(number As Double, dec As Integer) As Double
    Dim decimalPowerOfTen = Math.Pow(10, dec)
    If CInt(number * decimalPowerOfTen) = Math.Round(number * decimalPowerOfTen, 2) Then
        Return Math.Round(number, 2, MidpointRounding.AwayFromZero)
    Else
        Return CInt(number * decimalPowerOfTen + 0.5) / 100
    End If
End Function

尝试使用1.905和2个小数将如预期的那样给出1.91,但Math.Round(1.905,2,MidpointRounding.AwayFromZero)给出1.90!数学。对于程序员可能遇到的大多数基本问题,Round方法是绝对不一致和不可用的。我必须检查如果(int) 1.905 * decimalPowerOfTen =数学。四舍五入(数字* decimalPowerOfTen, 2)因为我不想四舍五入什么应该四舍五入下来。

从MSDN:

默认为Math。圆的使用 MidpointRounding.ToEven。大多数人 不熟悉“四舍五入? 甚至“作为替代”,四舍五入 “远离零”更常见 . net默认为 “四舍五入到偶数” 统计上的优势是因为 没有分享的倾向 四舍五入的意思是四舍五入 比它循环的频率稍高 向下(假设数字为 四舍五入往往是积极的。)

http://msdn.microsoft.com/en-us/library/system.math.round.aspx