C# 四舍五入 MidpointRounding.AwayFromZero

本文介绍了C#中使用Math.Round进行四舍五入的方法,特别是通过传入MidpointRounding.AwayFromZero参数来确保四舍五入的准确性。示例代码展示了不同情况下四舍五入的结果,帮助开发者理解如何在实践中正确应用这一技巧。
摘要由CSDN通过智能技术生成

四舍五入 在计算中 经常使用到,但是如果使用 Math.Round,只是五舍六入
在Math.Round内传入MidpointRounding.AwayFromZero枚举,就可以实现四舍五入的效果了,

            Debug.Log($"四舍五入{66.6}。。。{(int)Math.Round(66.6, MidpointRounding.AwayFromZero)}");
            Debug.Log($"四舍五入{66.5}。。。{(int)Math.Round(66.5, MidpointRounding.AwayFromZero)}");
            Debug.Log($"四舍五入{66.4}。。。{(int)Math.Round(66.4, MidpointRounding.AwayFromZero)}");

            Debug.Log($"四舍五入{66.6}。。。{(int)Math.Round(66.6)}");
            Debug.Log($"四舍五入{66.5}。。。{(int)Math.Round(66.5)}");
            Debug.Log($"四舍五入{66.4}。。。{(int)Math.Round(66.4)}");

在这里插入图片描述
C#文档
https://docs.microsoft.com/zh-cn/dotnet/api/system.midpointrounding?view=net-6.0#system-midpointrounding-awayfromzero

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值