C# 中的四舍五入算法及 MSDN 2005 中文版 Math.Round 勘误 .
2011-07-13 11:41
393 查看
C# 中没有四舍五入函数,事实上我知道的程序语言都没有四舍五入函数,因为四舍五入算法不科学,国际通行的是 Banker 舍入法。
Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的。Banker 舍入法在大量样本统计计算中精度明显比四舍五入高,因为四舍五入总是逢五进位,除非样本中保留精度位不出现5,否则结果必然偏大。
Math.Round 方法默认的也是 Banker 舍入法。
在 .NET 2.0 中 Math.Round 方法有几个重载方法:
Math.Round(Decimal, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Double, Int32, MidpointRounding)
将小数值舍入到指定精度。MidpointRounding 参数,指定当一个值正好处于另两个数中间时如何舍入这个值。
该参数是个 MidpointRounding 枚举。
此枚举有两个成员,MSDN 中的说明是:
AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
注 意!这里关于 MidpointRounding.AwayFromZero 的说明是错误的!实际舍入为两个值中绝对值较大的值。不过 MSDN 中的例子是正确的,英文描述原文是 it is rounded toward the nearest number that is away from zero.
所以,要实现四舍五入函数,对于正数,可以加一个 MidpointRounding.AwayFromZero 参数指定当一个数字是其他两个数字的中间值时其舍入为两个值中绝对值较大的值,例:
Math.Round(3.45, 2, MidpointRounding.AwayFromZero)
/article/8694540.html
Banker's rounding(银行家舍入)算法,即四舍六入五取偶。事实上这也是 IEEE 规定的舍入标准。因此所有符合 IEEE 标准的语言都应该是采用这一算法的。Banker 舍入法在大量样本统计计算中精度明显比四舍五入高,因为四舍五入总是逢五进位,除非样本中保留精度位不出现5,否则结果必然偏大。
Math.Round 方法默认的也是 Banker 舍入法。
在 .NET 2.0 中 Math.Round 方法有几个重载方法:
Math.Round(Decimal, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Double, Int32, MidpointRounding)
将小数值舍入到指定精度。MidpointRounding 参数,指定当一个值正好处于另两个数中间时如何舍入这个值。
该参数是个 MidpointRounding 枚举。
此枚举有两个成员,MSDN 中的说明是:
AwayFromZero 当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
ToEven 当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
注 意!这里关于 MidpointRounding.AwayFromZero 的说明是错误的!实际舍入为两个值中绝对值较大的值。不过 MSDN 中的例子是正确的,英文描述原文是 it is rounded toward the nearest number that is away from zero.
所以,要实现四舍五入函数,对于正数,可以加一个 MidpointRounding.AwayFromZero 参数指定当一个数字是其他两个数字的中间值时其舍入为两个值中绝对值较大的值,例:
Math.Round(3.45, 2, MidpointRounding.AwayFromZero)
/article/8694540.html
相关文章推荐
- C# 中的四舍五入算法及 MSDN 2005 中文版 Math.Round 勘误
- C#中Math.Round()实现中国式四舍五入
- C#练习记录(请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round())
- WinCE的C#编程,对float型进行四舍五入保留两位小数,小数进行四舍五入操作,Math.Round的应用案例。
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#使用Math.Round()四舍五入
- C#中Math.Round()实现中国式四舍五入
- c#Math.Round最準確的四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- Javascript四舍五入(Math.round()与Math.pow())
- Javascript四舍五入Math.round()与Math.pow()使用介绍
- Javascript四舍五入(Math.round()与Math.pow())
- Refactoring C# Code Using Visual Studio 2005 ----MSDN
- c# 保留小数----关于System.Math.Round()的用法