• 聊一聊c#中的MATH.ROUND方法


    MATH.ROUND方法微软的官方解释是:将值舍入到最接近的整数或指定的小数位数。

    该方法有如下8个重载:

            /// <summary>
            /// 将小数值舍入到最接近的整数值。
            /// </summary>
            /// <param name="d">要舍入的小数。</param>
            /// <returns>最接近参数 d 的整数。如果 d 的小数部分正好处于两个整数中间,其中一个整数为偶数,另一个整数为奇数,则返回偶数。请注意,此方法返回 System.Decimal,而不是整数类型。</returns>
            decimal Round(decimal d);
            /// <summary>
            /// 将双精度浮点值舍入为最接近的整数值。
            /// </summary>
            /// <param name="a">要舍入的双精度浮点数。</param>
            /// <returns>最接近 a 的整数。如果 a 的小数部分正好处于两个整数中间,其中一个整数为偶数,另一个整数为奇数,则返回偶数。请注意,此方法返回 System.Double,而不是整数类型。</returns>
            double Round(double a);
            /// <summary>
            ///  将小数值舍入到最接近的整数。一个参数,指定当一个值正好处于两个数中间时如何舍入这个值。
            /// </summary>
            /// <param name="d">要舍入的小数。</param>
            /// <param name="mode">在两个数字之间时如何舍入 d 的规范。</param>
            /// <returns>最接近 d 的整数。如果 d 是两个数字的中值,这两个数字一个为偶数,另一个为奇数,则 mode 确定返回两个数字中的哪一个。</returns>
            decimal Round(decimal d, MidpointRounding mode);
            /// <summary>
            /// 将小数值按指定的小数位数舍入。
            /// </summary>
            /// <param name="d">要舍入的小数。</param>
            /// <param name="decimals">返回值中的小数位数。</param>
            /// <returns>最接近 d 的 decimals 位小数的数字。</returns>
            decimal Round(decimal d, int decimals);
            /// <summary>
            /// 将双精度浮点值舍入为最接近的整数。一个参数,指定当一个值正好处于两个数中间时如何舍入这个值。
            /// </summary>
            /// <param name="value">要舍入的双精度浮点数。</param>
            /// <param name="mode">在两个数字之间时如何舍入 value 的规范。</param>
            /// <returns>最接近 value 的整数。如果 value 是两个整数的中值,这两个整数一个为偶数,另一个为奇数,则 mode 确定返回两个整数中的哪一个。</returns>
            double Round(double value, MidpointRounding mode);
            /// <summary>
            /// 将双精度浮点值按指定的小数位数舍入。
            /// </summary>
            /// <param name="value">要舍入的双精度浮点数。</param>
            /// <param name="digits">返回值中的小数数字。</param>
            /// <returns></returns>
            double Round(double value, int digits);
            /// <summary>
            /// 将双精度浮点值按指定的小数位数舍入。一个参数,指定当一个值正好处于两个数中间时如何舍入这个值。
            /// </summary>
            /// <param name="value">要舍入的双精度浮点数。</param>
            /// <param name="digits">返回值中的小数数字。</param>
            /// <param name="mode">在两个数字之间时如何舍入 value 的规范。</param>
            /// <returns>最接近 value 的 digits 位小数的数字。如果 value 比 digits 少部分数字,value 原样返回。</returns>
            double Round(double value, int digits, MidpointRounding mode);
            /// <summary>
            /// 将小数值按指定的小数位数舍入。一个参数,指定当一个值正好处于两个数中间时如何舍入这个值。
            /// </summary>
            /// <param name="d">要舍入的小数。</param>
            /// <param name="decimals">返回值中的小数位数。</param>
            /// <param name="mode">在两个数字之间时如何舍入 d 的规范。</param>
            /// <returns>最接近 d 的 decimals 位小数的数字。如果 d 比 decimals 少部分数字,d 原样返回。</returns>
            decimal Round(decimal d, int decimals, MidpointRounding mode);

    其中参数MidpointRounding是个枚举,对应的值为:

     现将各个重载方法一一实现如下:

    1、得到整数

      1.1、double Round(double a)输出结果为:

      

       1.2、decimal Round(decimal d)输出结果为:

      

       1.3、decimal Round(decimal d, int decimals)输出结果为:

      

       1.4、double Round(double value, int digits)输出结果为:

      

       1.5、decimal Round(decimal d, MidpointRounding mode)输出结果为:

      

       1.6、double Round(double value, MidpointRounding mode)输出结果:

      

       1.7、double Round(double value, int digits, MidpointRounding mode)输出结果:

      

       1.8、decimal Round(decimal d, int decimals, MidpointRounding mode)输出结果:

      

     2、得到两位小数

      2.1、decimal Round(decimal d, int decimals)输出结果:

      

       2.2、double Round(double value, int digits)输出结果:

      

       2.3、double Round(double value, int digits, MidpointRounding mode)输出结果:

      

       2.4、decimal Round(decimal d, int decimals, MidpointRounding mode)输出结果:

      

     根据以上运行结果,我们在使用Round处理数据时,不能随便使用,要根据自己的具体需求来选择对应的方法,特别是我们对数据进行四舍五入时,要选对适合自己项目需要的方法是很重要的。如果随意使用,可能得到意想不到的结果。

      可运行控制台代码:

    #region 整数
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*********************************整数输出*********************************");
                Console.ForegroundColor = ConsoleColor.Red;
                //double Round(double a);
                Console.WriteLine($"Math.Round(0.4)输出结果:{Math.Round(0.4)}");
                Console.WriteLine($"Math.Round(0.5)输出结果:{Math.Round(0.5)}");
                Console.WriteLine($"Math.Round(0.501)输出结果:{Math.Round(0.501)}");
                Console.WriteLine($"Math.Round(0.6)输出结果:{Math.Round(0.6)}");
                Console.WriteLine($"Math.Round(1.5)输出结果:{Math.Round(1.5)}");
                Console.WriteLine($"Math.Round(2.5)输出结果:{Math.Round(2.5)}");
                Console.WriteLine($"Math.Round(3.5)输出结果:{Math.Round(3.5)}");
                Console.WriteLine($"Math.Round(4.5)输出结果:{Math.Round(4.5)}");
                Console.WriteLine($"Math.Round(5.5)输出结果:{Math.Round(5.5)}");
                Console.WriteLine($"Math.Round(6.5)输出结果:{Math.Round(6.5)}");
                Console.WriteLine($"Math.Round(7.5)输出结果:{Math.Round(7.5)}");
                Console.WriteLine($"Math.Round(8.5)输出结果:{Math.Round(8.5)}");
                Console.WriteLine($"Math.Round(9.5)输出结果:{Math.Round(9.5)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("***************Math.Round(double)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                //decimal Round(decimal d);
                Console.WriteLine($"Math.Round((decimal)0.4)输出结果:{Math.Round((decimal)0.4)}");
                Console.WriteLine($"Math.Round((decimal)0.5)输出结果:{Math.Round((decimal)0.5)}");
                Console.WriteLine($"Math.Round((decimal)0.501)输出结果:{Math.Round((decimal)0.501)}");
                Console.WriteLine($"Math.Round((decimal)0.6)输出结果:{Math.Round((decimal)0.6)}");
                Console.WriteLine($"Math.Round((decimal)1.5)输出结果:{Math.Round((decimal)1.5)}");
                Console.WriteLine($"Math.Round((decimal)2.5)输出结果:{Math.Round((decimal)2.5)}");
                Console.WriteLine($"Math.Round((decimal)3.5)输出结果:{Math.Round((decimal)3.5)}");
                Console.WriteLine($"Math.Round((decimal)4.5)输出结果:{Math.Round((decimal)4.5)}");
                Console.WriteLine($"Math.Round((decimal)5.5)输出结果:{Math.Round((decimal)5.5)}");
                Console.WriteLine($"Math.Round((decimal)6.5)输出结果:{Math.Round((decimal)6.5)}");
                Console.WriteLine($"Math.Round((decimal)7.5)输出结果:{Math.Round((decimal)7.5)}");
                Console.WriteLine($"Math.Round((decimal)8.5)输出结果:{Math.Round((decimal)8.5)}");
                Console.WriteLine($"Math.Round((decimal)9.5)输出结果:{Math.Round((decimal)9.5)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**************Math.Round(decimal)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //decimal Round(decimal d, int decimals);
                Console.WriteLine($"Math.Round((decimal)0.4,0)输出结果:{Math.Round((decimal)0.4, 0)}");
                Console.WriteLine($"Math.Round((decimal)0.5, 0)输出结果:{Math.Round((decimal)0.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)0.501, 0)输出结果:{Math.Round((decimal)0.501, 0)}");
                Console.WriteLine($"Math.Round((decimal)0.6, 0)输出结果:{Math.Round((decimal)0.6, 0)}");
                Console.WriteLine($"Math.Round((decimal)1.5, 0)输出结果:{Math.Round((decimal)1.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)2.5, 0)输出结果:{Math.Round((decimal)2.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)3.5, 0)输出结果:{Math.Round((decimal)3.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)4.5, 0)输出结果:{Math.Round((decimal)4.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)5.5, 0)输出结果:{Math.Round((decimal)5.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)6.5, 0)输出结果:{Math.Round((decimal)6.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)7.5, 0)输出结果:{Math.Round((decimal)7.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)8.5, 0)输出结果:{Math.Round((decimal)8.5, 0)}");
                Console.WriteLine($"Math.Round((decimal)9.5, 0)输出结果:{Math.Round((decimal)9.5, 0)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**************Math.Round(decimal,int)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                //double Round(double value, int digits);
                Console.WriteLine($"Math.Round(0.4,0)输出结果:{Math.Round(0.4, 0)}");
                Console.WriteLine($"Math.Round(0.5, 0)输出结果:{Math.Round(0.5, 0)}");
                Console.WriteLine($"Math.Round(0.501, 0)输出结果:{Math.Round(0.501, 0)}");
                Console.WriteLine($"Math.Round(0.6, 0)输出结果:{Math.Round(0.6, 0)}");
                Console.WriteLine($"Math.Round(1.5, 0)输出结果:{Math.Round(1.5, 0)}");
                Console.WriteLine($"Math.Round(2.5, 0)输出结果:{Math.Round(2.5, 0)}");
                Console.WriteLine($"Math.Round(3.5, 0)输出结果:{Math.Round(3.5, 0)}");
                Console.WriteLine($"Math.Round(4.5, 0)输出结果:{Math.Round(4.5, 0)}");
                Console.WriteLine($"Math.Round(5.5, 0)输出结果:{Math.Round(5.5, 0)}");
                Console.WriteLine($"Math.Round(6.5, 0)输出结果:{Math.Round(6.5, 0)}");
                Console.WriteLine($"Math.Round(7.5, 0)输出结果:{Math.Round(7.5, 0)}");
                Console.WriteLine($"Math.Round(8.5, 0)输出结果:{Math.Round(8.5, 0)}");
                Console.WriteLine($"Math.Round(9.5, 0)输出结果:{Math.Round(9.5, 0)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**************Math.Round(double,int)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //decimal Round(decimal d, MidpointRounding mode);
                Console.WriteLine($"Math.Round((decimal)0.4, MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.4, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.501,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.501, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.6,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.6, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)1.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)1.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)2.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)2.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)3.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)3.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)4.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)4.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)5.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)5.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)6.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)6.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)7.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)7.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)8.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)8.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)9.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)9.5, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,MidpointRounding.AwayFromZero)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round((decimal)0.4, MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.4, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.501,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.501, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.6,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.6, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)1.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)1.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)2.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)2.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)3.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)3.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)4.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)4.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)5.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)5.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)6.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)6.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)7.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)7.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)8.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)8.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)9.5,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)9.5, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,MidpointRounding.ToEven)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
    
                //double Round(double value, MidpointRounding mode);
                Console.WriteLine($"Math.Round(0.4, MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.4, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.501,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.501, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.6,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.6, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(1.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(1.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(2.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(2.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(3.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(3.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(4.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(4.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(5.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(5.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(6.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(6.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(7.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(7.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(8.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(8.5, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(9.5,MidpointRounding.AwayFromZero)输出结果:{Math.Round(9.5, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,MidpointRounding.AwayFromZero)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round(0.4, MidpointRounding.ToEven)输出结果:{Math.Round(0.4, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.5,MidpointRounding.ToEven)输出结果:{Math.Round(0.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.501,MidpointRounding.ToEven)输出结果:{Math.Round(0.501, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.6,MidpointRounding.ToEven)输出结果:{Math.Round(0.6, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(1.5,MidpointRounding.ToEven)输出结果:{Math.Round(1.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(2.5,MidpointRounding.ToEven)输出结果:{Math.Round(2.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(3.5,MidpointRounding.ToEven)输出结果:{Math.Round(3.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(4.5,MidpointRounding.ToEven)输出结果:{Math.Round(4.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(5.5,MidpointRounding.ToEven)输出结果:{Math.Round(5.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(6.5,MidpointRounding.ToEven)输出结果:{Math.Round(6.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(7.5,MidpointRounding.ToEven)输出结果:{Math.Round(7.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(8.5,MidpointRounding.ToEven)输出结果:{Math.Round(8.5, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(9.5,MidpointRounding.ToEven)输出结果:{Math.Round(9.5, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,MidpointRounding.ToEven)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //double Round(double value, int digits, MidpointRounding mode);
                Console.WriteLine($"Math.Round(0.4,0, MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.4, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.501,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.501, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.6,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.6, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(1.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(1.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(2.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(2.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(3.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(3.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(4.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(4.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(5.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(5.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(6.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(6.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(7.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(7.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(8.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(8.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(9.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round(9.5, 0, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,int,MidpointRounding.AwayFromZero)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round(0.4,0, MidpointRounding.ToEven)输出结果:{Math.Round(0.4, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(0.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.501,0,MidpointRounding.ToEven)输出结果:{Math.Round(0.501, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.6,0,MidpointRounding.ToEven)输出结果:{Math.Round(0.6, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(1.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(1.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(2.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(2.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(3.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(3.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(4.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(4.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(5.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(5.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(6.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(6.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(7.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(7.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(8.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(8.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(9.5,0,MidpointRounding.ToEven)输出结果:{Math.Round(9.5, 0, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,int,MidpointRounding.ToEven)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //decimal Round(decimal d, int decimals, MidpointRounding mode);
                Console.WriteLine($"Math.Round((decimal)0.4,0, MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.4, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.501,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.501, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.6,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.6, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)1.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)1.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)2.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)2.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)3.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)3.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)4.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)4.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)5.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)5.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)6.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)6.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)7.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)7.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)8.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)8.5, 0, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)9.5,0,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)9.5, 0, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,int,MidpointRounding.AwayFromZero)*END***************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round((decimal)0.4,0, MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.4, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.501,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.501, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.6,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.6, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)1.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)1.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)2.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)2.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)3.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)3.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)4.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)4.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)5.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)5.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)6.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)6.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)7.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)7.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)8.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)8.5, 0, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)9.5,0,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)9.5, 0, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,int,MidpointRounding.ToEven)*END*********************");
    
    
                #endregion
    
                #region 两位小数
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*********************************两位小数*********************************");
                Console.ForegroundColor = ConsoleColor.Red;
                //decimal Round(decimal d, int decimals);
                Console.WriteLine($"Math.Round((decimal)0.464,2)输出结果:{Math.Round((decimal)0.464, 2)}");
                Console.WriteLine($"Math.Round((decimal)0.465, 2)输出结果:{Math.Round((decimal)0.465, 2)}");
                Console.WriteLine($"Math.Round((decimal)0.46501, 2)输出结果:{Math.Round((decimal)0.46501, 2)}");
                Console.WriteLine($"Math.Round((decimal)0.466, 2)输出结果:{Math.Round((decimal)0.466, 2)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**************Math.Round(decimal,int)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                //double Round(double value, int digits);
                Console.WriteLine($"Math.Round(0.464,2)输出结果:{Math.Round(0.464, 2)}");
                Console.WriteLine($"Math.Round(0.465, 2)输出结果:{Math.Round(0.465, 2)}");
                Console.WriteLine($"Math.Round(0.46501, 2)输出结果:{Math.Round(0.46501, 2)}");
                Console.WriteLine($"Math.Round(0.466, 2)输出结果:{Math.Round(0.466, 2)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("**************Math.Round(double,int)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //double Round(double value, int digits, MidpointRounding mode);
                Console.WriteLine($"Math.Round(0.464,2, MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.464, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.465,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.465, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.46501,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.46501, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round(0.466,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round(0.466, 2, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,int,MidpointRounding.AwayFromZero)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round(0.464,2, MidpointRounding.ToEven)输出结果:{Math.Round(0.464, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.465,2,MidpointRounding.ToEven)输出结果:{Math.Round(0.465, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.46501,2,MidpointRounding.ToEven)输出结果:{Math.Round(0.46501, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round(0.466,2,MidpointRounding.ToEven)输出结果:{Math.Round(0.466, 2, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(double,int,MidpointRounding.ToEven)*END*********************");
                Console.ForegroundColor = ConsoleColor.Red;
    
                //decimal Round(decimal d, int decimals, MidpointRounding mode);
                Console.WriteLine($"Math.Round((decimal)0.464,2, MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.464, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.465,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.465, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.46501,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.46501, 2, MidpointRounding.AwayFromZero)}");
                Console.WriteLine($"Math.Round((decimal)0.466,2,MidpointRounding.AwayFromZero)输出结果:{Math.Round((decimal)0.466, 2, MidpointRounding.AwayFromZero)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,int,MidpointRounding.AwayFromZero)*END***************");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Math.Round((decimal)0.464,2, MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.464, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.465,2,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.465, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.46501,2,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.46501, 2, MidpointRounding.ToEven)}");
                Console.WriteLine($"Math.Round((decimal)0.466,2,MidpointRounding.ToEven)输出结果:{Math.Round((decimal)0.466, 2, MidpointRounding.ToEven)}");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("*****Math.Round(decimal,int,MidpointRounding.ToEven)*END*********************");
                #endregion
                
                Console.ReadKey();
    View Code

    参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.math.round?redirectedfrom=MSDN&view=netframework-4.7.2

     

  • 相关阅读:
    三种构建器的使用
    java class file
    JVM知识(下)
    JVM知识(上)
    初识JMM
    nginx基础配置加基础实战演示
    GitHub中webhooks的使用
    redis sentinel介绍
    redis主从复制
    【开源】.net 分布式架构之监控平台
  • 原文地址:https://www.cnblogs.com/fgq520328/p/12107335.html
Copyright © 2020-2023  润新知