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
參考:
相關用法
- C# Dictionary.Add()用法及代碼示例
- C# Math.Max()用法及代碼示例
- C# Math.Min()用法及代碼示例
- C# Decimal.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Exp()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Decimal.Round() Method in C# | Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。