C# Decimal.Round()方法实例讲解

时间:2022-04-06
本文章向大家介绍C# Decimal.Round()方法实例讲解,主要分析其语法、参数、返回值和注意事项,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Decimal.Round方法用于将值舍入到最接近的整数或指定数量的小数位数。此方法的重载列表中有4种方法,如下所示:

    • Round(Decimal) 方法
    • Round(Decimal,Int32)方法
    • Round(Decimal, MidpointRounding) 方法
    • Round(Decimal,Int32,MidpointRounding)方法


在这里,我们将讨论前两种方法。

Decimal.Round(Decimal) Method

此方法用于将十进制值舍入到最接近的整数。

用法: public static decimal Round (decimal d);
Here, it takes a decimal number to round.

返回值:此方法返回最接近d参数的整数。如果d在两个整数之间,其中一个是偶数,另一个是奇数,则返回偶数。

异常:如果结果超出Decimal值的范围,则此方法将引发OverflowException。

以下示例程序旨在说明Decimal.Round(Decimal)方法的用法:

范例1:

// C# program to demonstrate the 
// Decimal.Round(Decimal) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring and initializing value 
            decimal value = 184467440737095.51615M; 
  
            // getting rounded decimal 
            // using Round() method 
            decimal round = Decimal.Round(value); 
  
            // Display the value 
            Console.WriteLine("Rounded value is {0}", round); 
        } 
  
        catch (OverflowException e)  
        { 
            Console.WriteLine("Value must not be out of bound"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}

输出:

Rounded value is 184467440737096

范例2:对于OverflowException

// C# program to demonstrate the 
// Decimal.Round(Decimal) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try 
        { 
  
            // Declaring and initializing value 
            decimal value = 79228162514264337593543950335.5M; 
  
            // getting rounded decimal 
            // using Round() method 
            decimal round = Decimal.Round(value); 
  
            // Display the value 
            Console.WriteLine("Rounded value is {0}", round); 
        } 
  
        catch (OverflowException e)  
        { 
            Console.WriteLine("Value must not be out of bound"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}

Compile-Time错误:



prog.cs(15,51):error CS0594:Floating-point constant is outside the range of type `decimal’

Round(Decimal, Int32) Method

此方法用于将十进制值舍入到指定的小数位数。

用法: public static decimal Round (decimal d, int decimals);

参数:
d:它是一个十进制数,将被四舍五入。
decimals:从0到28的值,指定要舍入到的小数位数。

返回值:此方法返回等于d的十进制数,四舍五入为小数位的小数位数。

异常:如果小数位数不是0到28之间的值,则此方法将引发ArgumentOutOfRangeException。

以下示例程序旨在说明Decimal.Round(Decimal,Int32)方法的用法:

范例1:

// C# program to demonstrate the 
// Decimal.Round(Decimal) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // Declaring and initializing value 
            decimal value = 7922816251426433759354.39503305M; 
  
            // getting rounded decimal 
            // using Round() method 
            decimal round = Decimal.Round(value, 4); 
  
            // Display the value 
            Console.WriteLine("Rounded value is {0}", round); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.WriteLine("decimal place is not within bound"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}

输出:

Rounded value is 7922816251426433759354.3950

范例2:对于ArgumentOutOfRangeException

// C# program to demonstrate the 
// Decimal.Round(Decimal) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try 
        { 
  
            // Declaring and initializing value 
            decimal value = 7922816251426433759354.39503305M; 
  
            // getting rounded decimal 
            // using Round() method 
            decimal round = Decimal.Round(value, 29); 
  
            // Display the value 
            Console.WriteLine("Rounded value is {0}", round); 
        } 
  
        catch (ArgumentOutOfRangeException e)  
        { 
            Console.WriteLine("Decimal place is not within bound"); 
            Console.Write("Exception Thrown:"); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}

输出:

Decimal place is not within bound
Exception Thrown:System.ArgumentOutOfRangeException

参考: