在C#中,Math.Round(2.5)
的结果是2。
这不应该是3吗?为什么在C#中结果却是2呢?
Silverlight不支持MidpointRounding选项。 这里是一个为Silverlight添加MidpointRounding枚举的扩展方法:
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/
.NET四舍五入具有您正在寻找的答案。
基本上它说:
返回值
最接近数字且精度等于digits的值。如果value恰好位于两个数字之间,其中一个是偶数,另一个是奇数,则返回偶数。 如果value的精度小于digits,则返回未更改的value。
此方法的行为遵循IEEE标准754第4节。这种舍入有时称为最近舍入或银行家舍入。如果digits为零,则有时称为朝零舍入。
public int Round(double value)
{
double decimalpoints = Math.Abs(value - Math.Floor(value));
if (decimalpoints > 0.5)
return (int)Math.Round(value);
else
return (int)Math.Floor(value);
}
>.5
产生与 Math.Round
相同的行为。问题是当小数部分恰好为 0.5
时会发生什么。Math.Round
允许您指定所需的舍入算法。 - Panagiotis Kanavos这是我不得不解决它的方法:
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
尝试使用2位小数的1.905将得到预期的1.91,但是Math.Round(1.905,2,MidpointRounding.AwayFromZero)
却给出了1.90!Math.Round方法在大多数程序员可能遇到的基本问题中是绝对不一致和无法使用的。我必须检查(int) 1.905 * decimalPowerOfTen = Math.Round(number * decimalPowerOfTen, 2)
是否会导致我不想四舍五入的向下取整。
Math.Round(1.905,2,MidpointRounding.AwayFromZero)
returns 1.91
- Panagiotis Kanavos这很丑陋,但总是产生正确的算术舍入。
public double ArithRound(double number,int places){
string numberFormat = "###.";
numberFormat = numberFormat.PadRight(numberFormat.Length + places, '#');
return double.Parse(number.ToString(numberFormat));
}
Math.Round
并指定所需的舍入方式,可以实现四舍五入。 - configurator
Math.Round(2.5, 0, MidpointRounding.AwayFromZero);
- Robert DurginDecimal
类型,这个问题就会消失。在我的看法中,Math.Round 方法重载,以双精度数字的小数位数为参数,是一个值得怀疑的设计选择,因为它很少能够有效地工作。 - CodesInChaos