Math.Round 之关于 不同的四舍五入
来源:互联网 发布:mysql5.7 for mac下载 编辑:程序博客网 时间:2024/06/09 16:44
之前有一直被误导,以为就是简单的四舍五入。
写这篇文章之前也是搜索一下,博客园中也是很多写过关于的文章。
本以为是很简单的就过去了,却发现了一个很不同现象。下面等我慢慢道来。
Math.Round有好多个重载,如下:
[MethodImplAttribute(MethodImplOptions.InternalCall)]public static extern double Round(double a); public static double Round(double value, int digits){ if ((digits < 0) || (digits > maxRoundingDigits)) throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits")); return InternalRound(value, digits, MidpointRounding.ToEven); } public static double Round(double value, MidpointRounding mode) { return Round(value, 0, mode);}public static double Round(double value, int digits, MidpointRounding mode) { if ((digits < 0) || (digits > maxRoundingDigits)) throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits")); if (mode < MidpointRounding.ToEven || mode > MidpointRounding.AwayFromZero) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidEnumValue", mode, "MidpointRounding"), "mode"); } return InternalRound(value, digits, mode);}public static Decimal Round(Decimal d) { return Decimal.Round(d,0);} public static Decimal Round(Decimal d, int decimals) { return Decimal.Round(d,decimals); }public static Decimal Round(Decimal d, MidpointRounding mode) { return Decimal.Round(d, 0, mode); } public static Decimal Round(Decimal d, int decimals, MidpointRounding mode) { return Decimal.Round(d, decimals, mode);}
由于翻了源代码,就直接贴了源代码。
这里是有两个版本的,double和decimal版本的,注意这两个版本的代码最终调用的方法可是不一样的哦,后面的现象原因就出在这里。
先来简单的例子:
Math.Round(12.5)
返回的是12,这个在搜索出来的资料是没有问题,可以解释为Banker 's rounding(银行家舍入)算法,即四舍六入五取偶,是IEEE规定的舍入标准。
当然我们需要四舍五入的话,需要设置 MidpointRounding .Math.Round(12.5, MidpointRounding.AwayFromZero)
则返回13。
来看一下MidpointRounding的枚举介绍(MSDN)。
ToEven
当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。
AwayFromZero
当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。
在默认情况下都是使用ToEven,在上面的代码中可以发现。
所以一开始以为只要使用AwayFromZero就可以完成四舍五入了。
下面再来一个例子:
Math.Round(0.145, 2, MidpointRounding.AwayFromZero)
Math.Round(0.145, 2, MidpointRounding.ToEven)
我想很多同学都会觉得第一个返回0.15,第二个返回0.14.
问题来了,结果不是这样的哦。正确结果两个都是0.14。 哇靠,AwayFromZero没起作用,没给我四舍五入!
不知道还有没有其他存在这样的例子,看样子AwayFromZero也不可靠啊。
其实这里是调用double的方法。
下面来试一下decimal的方法:
Math.Round((decimal)0.145, 2, MidpointRounding.AwayFromZero)
Math.Round((decimal)0.145, 2, MidpointRounding.ToEven)
这回结果与上面的版本不一样哦,得到了我想要的0.15和0.14。
不知道是不是.Net Framework的Math.Round的double版本一个bug呢?
1 0
- Math.Round 之关于 不同的四舍五入
- Math.Round 之关于 不同的四舍五入
- Math.Round 不同的四舍五入
- Math.round()的四舍五入问题
- Math.round四舍五入的理解
- Math.Round()方法的四舍五入(.NET)
- java Math.round()比较特殊的四舍五入
- Math.Round函数四舍五入的问题
- java Math.round()比较特殊的四舍五入
- Math.Round 四舍五入函数
- Math.round四舍五入
- 关于 java 中负数四舍五入的问题——Math.round()
- Javascript四舍五入(Math.round()与Math.pow())
- Javascript四舍五入(Math.round()与Math.pow())
- Javascript四舍五入(Math.round()与Math.pow())
- C#中Math.Round四舍五入的用法详解,
- C#中的Math.Round使用的"四舍五入"法
- js的向上取整(Math.ceil)向下取整(Math.floor)四舍五入(Math.round)
- Xcode多种Build Configuration配置使用
- 剑指offer-----栈的压入、弹出序列
- DbUnit入门实战
- jar 程序发布脚本
- Python之self
- Math.Round 之关于 不同的四舍五入
- C#操作文件及文件夹
- Android 使用Mockito测试业务逻辑<unittest 之一>
- Objective C 学习笔记
- ConcurrentHashMap原理
- 【Unity Shaders】学习笔记之固定着色器命令(二)
- Path Sum
- HMM原理介绍 示例 python代码实现
- Socket原理与编程基础