📜  C#中的Decimal.Round()方法套装– 1

📅  最后修改于: 2021-05-29 16:53:33             🧑  作者: Mango

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

  • 舍入(十进制)方法
  • Round(Decimal,Int32)方法
  • 舍入(十进制,中点舍入)方法
  • Round(Decimal,Int32,MidpointRounding)方法

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

Decimal.Round(Decimal)方法

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

下面的程序说明了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);
        }
    }
}

编译时错误:

Round(Decimal,Int32)方法

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

返回值:该方法返回等于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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.decimal.round?view=netframework-4.7.2