在c#中,Math.Round(2.5)的结果是2。
应该是3,不是吗?为什么在c#中它是2 ?
在c#中,Math.Round(2.5)的结果是2。
应该是3,不是吗?为什么在c#中它是2 ?
当前回答
使用. net对数字进行舍入可以得到您正在寻找的答案。
基本上它是这么说的:
返回值
精度等于数字的最接近的数字值。如果值位于两个数的中间,其中一个是偶数,另一个是奇数,则返回偶数。如果value的精度小于数字,则value不变地返回。
这种方法的行为遵循IEEE标准754,第4节。这种四舍五入有时被称为最接近四舍五入,或银行家四舍五入。如果数字为零,这种舍入有时被称为趋零舍入。
其他回答
因为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圆整
来自MSDN的数学。Round(double a)返回:
最接近a的整数 a的分数分量是一半 两个整数之间,其中一个是 偶数和另一个奇数,然后是偶数 返回Number。
... 因此,在2和3中间的2.5被四舍五入为偶数(2)。这被称为银行家四舍五入(或四舍五入为偶数),是一种常用的四舍五入标准。
同一篇MSDN文章:
此方法的行为如下 IEEE标准754,第4节。这 四舍五入有时被称为 四舍五入到最接近的,或银行家的 舍入。它最小化舍入误差 这是持续舍入的结果 一个单一的中点值 方向。
您可以通过调用Math的重载来指定不同的舍入行为。回合采取midpointround模式。
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/
以下是我必须解决的方法:
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)因为我不想四舍五入什么应该四舍五入下来。
舍入的性质
考虑一下将一个包含分数的数字四舍五入为整数的任务。在这种情况下,舍入的过程是确定哪个整数最能代表要舍入的数字。
在普通或“算术”四舍五入中,很明显2.1、2.2、2.3和2.4四舍五入到2.0;2.6 2.7 2.8 2.9到3.0。
剩下的是2.5,与2.0相比,它更接近3.0。2.0和3.0之间的选择取决于你,两者都是同样有效的。
对于负数,-2.1、-2.2、-2.3和-2.4会变成-2.0;而-2.6、2.7、2.8和2.9在算术四舍五入下会变成-3.0。
对于-2.5,需要在-2.0和-3.0之间进行选择。
其他形式的舍入
“四舍五入”取任何小数点后的数字,并使其成为下一个“整”数。因此,不仅2.5和2.6要四舍五入到3.0,2.1和2.2也要四舍五入到3.0。
四舍五入使正数和负数都远离零。2.5到3.0和-2.5到-3.0。
“舍入”通过砍掉不需要的数字来截断数字。这样做的效果是将数字移向零。2.5到2.0和-2.5到-2.0
在“银行家四舍五入”中——最常见的形式——要四舍五入的。5要么四舍五入,要么四舍五入,这样四舍五入的结果总是偶数。因此,2.5轮到2.0,3.5轮到4.0,4.5轮到4.0,5.5轮到6.0,以此类推。
'交替四舍五入'将任何。5的过程在四舍五入和四舍五入之间交替进行。
“随机舍入”是在完全随机的基础上舍入0.5上下的数值。
对称与不对称
一个舍入函数是“对称的”,如果它把所有的数字舍入到零,或者把所有的数字舍入到零。
如果将正数舍入为零,将负数舍入为零,则函数是“不对称的”。2.5到2.0;从-2.5到-3.0。
同样不对称的还有一个函数,它把正数舍入为零,把负数舍入为零。2.5至3.0;从-2.5到-2.0。
大多数时候,人们会想到对称舍入,即-2.5会舍入到-3.0,3.5会舍入到4.0。(在c# Round(AwayFromZero))