📜  C#| Math.Round()方法|套装– 1

📅  最后修改于: 2021-05-29 20:48:00             🧑  作者: Mango

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

  • 圆数(双)
  • Math.Round(Double,Int32)
  • Math.Round(十进制)
  • Math.Round(Decimal,Int32)
  • Math.Round(Double,Int32,MidpointRounding)
  • Math.Round(Double,MidpointRounding)
  • Math.Round(Decimal,Int32,MidpointRounding)
  • Math.Round(十进制,MidpointRounding)

圆数(双)

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

句法:

public static double Round(double x)

范围:

返回类型:返回最接近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 12
Rounded value of 12.634565 is 13

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

Math.Round(Double,Int32)

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

句法:

public static double Round(double x, Int32 y)

范围:

返回类型:返回最接近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.4346
Rounded value of 12.634565 is 12.63

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

Math.Round(十进制)

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

句法:

public static decimal Round(decimal x)

范围:

返回类型:返回最接近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.345
Rounded value of 12.345 is 12
Value of dec2 is 12.785
Rounded value of 12.785 is 13

Math.Round(Decimal,Int32)

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

句法:

public static decimal Round(decimal x, Int32 y)

范围:

返回类型:返回最接近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.223
Rounded value of 12.8734765 is 12.8735

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

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