Decimal.Round方法用于将值舍入到最接近的整数或指定数量的小数位数。此方法的重载列表中有4种方法,如下所示:
- 舍入(十进制)方法
- Round(Decimal,Int32)方法
- 舍入(十进制,中点舍入)方法
- Round(Decimal,Int32,MidpointRounding)方法
在这里,我们将讨论前两种方法。
Decimal.Round(Decimal)方法
此方法用于将十进制值舍入到最接近的整数。
Syntax: public static decimal Round (decimal d);
Here, it takes a decimal number to round.
Return Value: This method returns the integer which is nearest to the d parameter. If d is halfway between two integers, one of which is even and the other odd, the even number is returned.
Exceptions: This method throws OverflowException if the result is outside the range of a Decimal value.
下面的程序说明了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);
}
}
}
编译时错误:
prog.cs(15,51): error CS0594: Floating-point constant is outside the range of type `decimal’
Round(Decimal,Int32)方法
此方法用于将十进制值舍入到指定的小数位数。
Syntax: public static decimal Round (decimal d, int decimals);
Parameters:
d: It is a decimal number which is to be rounded.
decimals: It is a value from 0 to 28 that specifies the number of decimal places to round to.
返回值:该方法返回等于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