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

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


当前回答

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

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)因为我不想四舍五入什么应该四舍五入下来。

其他回答

这被称为舍入到偶数(或银行家舍入),这是一种有效的舍入策略,可以最大限度地减少总和中的累积错误(MidpointRounding.ToEven)。理论是,如果你总是在同一个方向四舍五入一个0.5的数字,误差会更快地累积(四舍五入到偶数应该是最小化)(a)。

以下是MSDN对以下内容的描述:

Math.Floor, which rounds down towards negative infinity. Math.Ceiling, which rounds up towards positive infinity. Math.Truncate, which rounds up or down towards zero. Math.Round, which rounds to the nearest integer or specified number of decimal places. You can specify the behavior if it's exactly equidistant between two possibilities, such as rounding so that the final digit is even ("Round(2.5,MidpointRounding.ToEven)" becoming 2) or so that it's further away from zero ("Round(2.5,MidpointRounding.AwayFromZero)" becoming 3).

下面的图表可能会有所帮助:

-3        -2        -1         0         1         2         3
 +--|------+---------+----|----+--|------+----|----+-------|-+
    a                     b       c           d            e

                       a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8
                       ======  ======  =====  =====  =====
Floor                    -3      -1      0      1      2
Ceiling                  -2       0      1      2      3
Truncate                 -2       0      0      1      2
Round(ToEven)            -3       0      0      2      3
Round(AwayFromZero)      -3      -1      0      2      3

请注意,Round比它看起来强大得多,因为它可以舍入到特定的小数点后数位。其他的都是0小数。例如:

n = 3.145;
a = System.Math.Round (n, 2, MidpointRounding.ToEven);       // 3.14
b = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15

对于其他函数,你必须使用乘除技巧来达到相同的效果:

c = System.Math.Truncate (n * 100) / 100;                    // 3.14
d = System.Math.Ceiling (n * 100) / 100;                     // 3.15

(a)当然,这个理论取决于这样一个事实,即你的数据在偶数一半(0.5,2.5,4.5,…)和奇数一半(1.5,3.5,…)之间的值分布相当均匀。

如果所有的“半值”都是偶数(例如),错误就会像你总是四舍五入一样快速累积。

首先,无论如何这都不是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),它提供了更多的选项。(它不只是处理中点。)

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

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

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

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

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

简单的方法是:

Math.Ceiling(decimal.Parse(yourNumber + ""));

我有这个问题,我的SQL服务器四舍五入0.5到1,而我的c#应用程序没有。所以你会看到两种不同的结果。

这是一个int/long的实现。这就是Java的舍入方法。

int roundedNumber = (int)Math.Floor(d + 0.5);

这可能也是你能想到的最有效的方法。

如果你想保持双精度并使用十进制精度,那么实际上只是使用基于小数点后多少位的10指数的问题。

public double getRounding(double number, int decimalPoints)
{
    double decimalPowerOfTen = Math.Pow(10, decimalPoints);
    return Math.Floor(number * decimalPowerOfTen + 0.5)/ decimalPowerOfTen;
}

你可以输入一个负的小数点作为小数点,这也很好。

getRounding(239, -2) = 200