C#中Math.Round()实现中国式四舍五入



 

C#中的Math.Round()并不是使用的"四舍五入"法。其实在VB、VBScript、C#、J#、T-SQL中Round函数都是采用Banker's rounding(银行家算法),即:四舍六入五取偶。事实上这也是IEEE的规范,因此所有符合IEEE标准的语言都应该采用这样的算法。

.NET 2.0 开始,Math.Round 方法提供了一个枚举选项 MidpointRounding.AwayFromZero 可以用来实现传统意义上的"四舍五入"。即: Math.Round(4.5, MidpointRounding.AwayFromZero) = 5。

复制代码
Round(Decimal)
Round(Double)
Round(Decimal, Int32)
Round(Decimal, MidpointRounding)
Round(Double, Int32)
Round(Double, MidpointRounding)
Round(Decimal, Int32, MidpointRounding)
Round(Double, Int32, MidpointRounding)
复制代码

 

 如:

Math.Round(0.4) //result:0

Math.Round(0.6) //result:1

Math.Round(0.5) //result:0

Math.Round(1.5) //result:2

Math.Round(2.5) //result:2

Math.Round(3.5) //result:4

Math.Round(4.5) //result:4

Math.Round(5.5) //result:6

Math.Round(6.5) //result:6

Math.Round(7.5) //result:8

Math.Round(8.5) //result:8

Math.Round(9.5) //result:10

   使用MidpointRounding.AwayFromZero重载后对比:   

Math.Round(0.4, MidpointRounding.AwayFromZero); // result:0

Math.Round(0.6, MidpointRounding.AwayFromZero); // result:1

Math.Round(0.5, MidpointRounding.AwayFromZero); // result:1

Math.Round(1.5, MidpointRounding.AwayFromZero); // result:2

Math.Round(2.5, MidpointRounding.AwayFromZero); // result:3

Math.Round(3.5, MidpointRounding.AwayFromZero); // result:4

Math.Round(4.5, MidpointRounding.AwayFromZero); // result:5

Math.Round(5.5, MidpointRounding.AwayFromZero); // result:6

Math.Round(6.5, MidpointRounding.AwayFromZero); // result:7

Math.Round(7.5, MidpointRounding.AwayFromZero); // result:8

Math.Round(8.5, MidpointRounding.AwayFromZero); // result:9

Math.Round(9.5, MidpointRounding.AwayFromZero); // result:10

   

   

但是悲剧的是,如果用这个计算小数的话,就不灵了!!!

必须用第七个重载方法,
decimal Round(decimal d, int decimals, MidpointRounding mode)

这样计算出来的小数才是真正的中国式四舍五入!!

    

复制代码
?Math.Round(526.925, 2)
526.92

?Math.Round(526.925, 2,MidpointRounding.AwayFromZero)
526.92

?Math.Round((decimal)526.925, 2)
526.92

?Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)
526.93
复制代码

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#Math.Round函数用于对一个数字进行四舍五入。在.NET 2.0及以后的版本Math.Round方法提供了一个枚举选项MidpointRounding.AwayFromZero,可以实现传统意义上的"四舍五入"。例如,Math.Round(4.5, MidpointRounding.AwayFromZero)的结果是5。\[1\] Math.Round函数有多个重载形式,可以接受不同的参数类型。其包括Round(Decimal)、Round(Double)、Round(Decimal, Int32)、Round(Decimal, MidpointRounding)、Round(Double, Int32)、Round(Double, MidpointRounding)、Round(Decimal, Int32, MidpointRounding)和Round(Double, Int32, MidpointRounding)等。\[2\] 在C#Math.Round函数的默认行为是使用"四舍六入五取偶"的规则。也就是说,当小数部分为5时,会根据前一位数字的奇偶性来决定舍入的方向。例如,Math.Round(0.5, 0)的结果是0,Math.Round(1.5, 0)的结果是2,Math.Round(2.5, 0)的结果是2,Math.Round(3.5, 0)的结果是4。\[3\] 总结来说,C#Math.Round函数可以用于对数字进行四舍五入,可以通过设置MidpointRounding.AwayFromZero实现传统的四舍五入规则。同时,还可以根据需要选择不同的重载形式来处理不同的参数类型。 #### 引用[.reference_title] - *1* *2* [C#Math.Round()](https://blog.csdn.net/qq_39956202/article/details/107837263)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C#-Math.Round()](https://blog.csdn.net/ldb987/article/details/75202976)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值