C# 四舍五入 MidpointRounding.AwayFromZero

四舍五入 在计算中 经常使用到,但是如果使用 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

猜你喜欢

转载自blog.csdn.net/o_ojjj/article/details/126223916