C# Math.Round方法实例讲解

发布时间:2025-12-09 11:48:57 浏览次数:1

在C#中,Math.Round()是Math类方法,用于将值四舍五入到最接近的整数或小数位数。可以通过更改传递的参数的数量和类型来重载此方法。 Math.Round()方法的重载列表中共有8种方法。在这里,我们将只讨论4种方法,其余4种方法将在C#中讨论。 Math.Round()方法|设置-2。

  • Math.Round(double)
  • Math.Round(Double,Int32)
  • Math.Round(decimal)
  • Math.Round(Decimal,Int32)
  • Math.Round(Double,Int32,MidpointRounding)
  • Math.Round(Double,MidpointRounding)
  • Math.Round(Decimal,Int32,MidpointRounding)
  • Math.Round(decimal,MidpointRounding)


Math.Round(Double)

此方法将双精度浮点值舍入为最接近的整数值。

用法:

public static double Round(double x)

参数:

  • x:要四舍五入的双浮点数。此参数的类型为System.Double。

返回类型:它返回最接近x的整数,返回类型为System.Double。

注意:如果x的小数部分位于两个整数的中间,其中一个为偶数,另一个为奇数,则返回偶数。

例:

// C# program to demonstrate the  // Math.Round(Double) method using System;   class Geeks {       // Main method     static void Main(string[] args)     {           // Case-1         // A double value whose fractional part is          // less than the halfway between two          // consecutive integers         Double dx1 = 12.434565d;           // Output value will be 12         Console.WriteLine("Rounded value of " + dx1 +                            " is " + Math.Round(dx1));           // Case-2         // A double value whose fractional part is          // greater than the halfway between two          // consecutive integers         Double dx2 = 12.634565d;           // Output value will be 13         Console.WriteLine("Rounded value of " + dx2 +                            " is " + Math.Round(dx2));     } }
输出:
Rounded value of 12.434565 is 12Rounded value of 12.634565 is 13

说明:在上面的代码中,假设用户想要将上述指定的double值四舍五入为最接近的整数。因此,编译器将首先检查该double值是否大于或小于该double数的偶数和奇数整数值。如果小于中途值,则其输出将为下限;否则,如果大于中途值,则其输出将为上限。



Math.Round(Double, Int32)

此方法将双精度浮点值舍入为指定数目的小数位数。

用法:

public static double Round(double x, Int32 y)

参数:

  • x:要四舍五入的双浮点数。此参数的类型为System.Double。 y:这是返回值中的小数位数。此参数的类型为System.Int32。

返回类型:它返回最接近x的整数,该整数包含与y相等的小数位数,返回类型为System.Double。

异常:如果y的值小于0或大于15,则此方法将提供ArgumentOutOfRangeException。

例:

// C# program to demonstrate the  // Math.Round(Double, Int32) method using System;    class Geeks {        // Main method     static void Main(string[] args)     {            // double type         Double dx1 = 12.434565d;            // using method         Console.WriteLine("Rounded value of " + dx1 +                            " is " + Math.Round(dx1, 4));            // double type         Double dx2 = 12.634565d;            // using method         Console.WriteLine("Rounded value of " + dx2 +                            " is " + Math.Round(dx2,2));     } }
输出:
Rounded value of 12.434565 is 12.4346Rounded value of 12.634565 is 12.63

说明:方法将检查指定的十进制数字旁边的数字是否大于或等于5。如果它大于或等于5,则它将递增前一个数字,否则前一个数字将保持不变。

Math.Round(Decimal)

此方法将精度为128位的十进制值四舍五入到最接近的整数值。



用法:

public static decimal Round(decimal x)

参数:

  • x:是要舍入的十进制数。此参数的类型为System.Decimal。

返回类型:它返回最接近x的整数,返回类型为System.Decimal。

注意:如果x的小数部分位于两个整数的中间,其中一个为偶数,另一个为奇数,则返回偶数。

例:

// C# program to demonstrate the  // Math.Round(Decimal) method using System;   class Geeks {       // Main method     static void Main(string[] args)     {           // Case-1         // A decimal value whose fractional part is          // less than the halfway between two          // consecutive integers         Decimal dec1 = 12.345m;           Console.WriteLine("Value of dec1 is " + dec1);           Console.WriteLine("Rounded value of " + dec1 +                            " is " + Math.Round(dec1));           // Case-2         // A double value whose fractional part is          // greater than the halfway between two          // consecutive integers         Decimal dec2 = 12.785m;           Console.WriteLine("Value of dec2 is " + dec2);           Console.WriteLine("Rounded value of " + dec2 +                            " is " + Math.Round(dec2));     } }
输出:
Value of dec1 is 12.345Rounded value of 12.345 is 12Value of dec2 is 12.785Rounded value of 12.785 is 13

Math.Round(Decimal, Int32)

此方法将十进制值舍入为指定数量的小数位数。

用法:

public static decimal Round(decimal x, Int32 y)

参数:



  • x:要舍入的十进制数。此参数的类型为System.Decimal。 y:这是返回值中的小数位数。此参数的类型为System.Int32。

返回类型:它返回最接近x的整数,该整数包含与y相等的小数位数,返回类型为System.Decimal。

异常:如果y的值小于0或大于15,则此方法将提供ArgumentOutOfRangeException;如果结果超出Decimal的范围,则此方法将提供OverflowException。

例:

// C# program to demonstrate the  // Math.Round(Decimal, Int32) method using System;   class Geeks {       // Main Method     static void Main(string[] args)     {           // Case - 1         // The value of digit after specified          // number is less than 5 & Here y = 3         Decimal dx1 = 12.2234565m;           // Output value will be 12.223         Console.WriteLine("Rounded value of " + dx1 +                            " is " + Math.Round(dx1, 3));           // Case - 2         // The value of digit after specified          // number is greater than 5 & Here y = 4         Decimal dx2 = 12.8734765m;           // Output value will be 12.8735         Console.WriteLine("Rounded value of " + dx2 +                            " is " + Math.Round(dx2, 4));       } }
输出:
Rounded value of 12.2234565 is 12.223Rounded value of 12.8734765 is 12.8735

注意:上面的十进制值代码以类似于Double值的方式工作。小数类型和双精度类型之间的唯一区别在于精度的概念,即在Double的情况下,精度为64位,在Decimal精度的情况下为128位。

参考: https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=netframework-4.7.2




math.round
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477