Math.Round 不同的四舍五入
来源:互联网 发布:软件的二次开发 编辑:程序博客网 时间:2024/05/01 02:40
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。
- 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四舍五入
- 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)
- math里面的四舍五入
- __attribute__
- 四种进程或线程同步互斥的控制方法
- 查询外网IP并发送邮件的脚本
- php 字符串 format 类
- IP地址和MAC地址互相查询
- Math.Round 不同的四舍五入
- TCL屏的问题
- sizeof 解析
- music player stop play music 1-2s when lcd enter suspend or awake state
- sql 半角 全角 传化函数
- 简要分析cpu-z和gpu-z软件
- 跳转页面
- MS-SQL数据库系统表的总结与应用
- Oracle创建自增字段方法