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

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


当前回答

首先,无论如何这都不是c#错误——而是。net错误。c#是一种语言——它不决定数学如何。实现了Round。

其次,不,如果你读了文档,你会发现默认的舍入是“舍入到偶数”(银行家的舍入):

Return ValueType: System.DoubleThe integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type. RemarksThe behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

你可以指定Math。Round应该使用重载对中点进行四舍五入,重载取midpointrsurround值。这里有一个带有midpointround的重载,对应于每个没有midpointround的重载:

圆(Decimal,中角) 圆(双)/圆(双,中点) 圆(Decimal, Int32) /圆(Decimal, Int32,中角肌) 圆(Double, Int32) /圆(Double, Int32,中角)

这种默认选择是否得当则是另一回事。(midpointround只是在。net 2.0中引入的。在此之前,我不确定是否有任何简单的方法来实现所需的行为,而不是自己动手。)特别是,历史已经表明,这不是预期的行为——在大多数情况下,这是API设计中的大忌。我知道为什么银行家舍入是有用的…但这对许多人来说仍然是一个惊喜。

您可能有兴趣看看最近的Java等效枚举(RoundingMode),它提供了更多的选项。(它不只是处理中点。)

其他回答

使用. net对数字进行舍入可以得到您正在寻找的答案。

基本上它是这么说的:

返回值

精度等于数字的最接近的数字值。如果值位于两个数的中间,其中一个是偶数,另一个是奇数,则返回偶数。如果value的精度小于数字,则value不变地返回。

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

这是非常丑陋的,但总是产生正确的算术四舍五入。

public double ArithRound(double number,int places){

  string numberFormat = "###.";

  numberFormat = numberFormat.PadRight(numberFormat.Length + places, '#');

  return double.Parse(number.ToString(numberFormat));

}

因为Silverlight不支持midpointrsurround选项,所以你必须自己编写。喜欢的东西:

public double RoundCorrect(double d, int decimals)
{
    double multiplier = Math.Pow(10, decimals);

    if (d < 0)
        multiplier *= -1;

    return Math.Floor((d * multiplier) + 0.5) / multiplier;

}

关于如何使用它作为扩展的例子,请参阅文章:.NET和Silverlight圆整

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/

首先,无论如何这都不是c#错误——而是。net错误。c#是一种语言——它不决定数学如何。实现了Round。

其次,不,如果你读了文档,你会发现默认的舍入是“舍入到偶数”(银行家的舍入):

Return ValueType: System.DoubleThe integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type. RemarksThe behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

你可以指定Math。Round应该使用重载对中点进行四舍五入,重载取midpointrsurround值。这里有一个带有midpointround的重载,对应于每个没有midpointround的重载:

圆(Decimal,中角) 圆(双)/圆(双,中点) 圆(Decimal, Int32) /圆(Decimal, Int32,中角肌) 圆(Double, Int32) /圆(Double, Int32,中角)

这种默认选择是否得当则是另一回事。(midpointround只是在。net 2.0中引入的。在此之前,我不确定是否有任何简单的方法来实现所需的行为,而不是自己动手。)特别是,历史已经表明,这不是预期的行为——在大多数情况下,这是API设计中的大忌。我知道为什么银行家舍入是有用的…但这对许多人来说仍然是一个惊喜。

您可能有兴趣看看最近的Java等效枚举(RoundingMode),它提供了更多的选项。(它不只是处理中点。)