Decimal.Round Method is used to round a value to the nearest integer or a specified number of decimal places. There are 4 methods in the overload list of this method as follows:
- Round(Decimal) Method
- Round(Decimal, Int32) Method
- Round(Decimal, MidpointRounding) Method
- Round(Decimal, Int32, MidpointRounding) Method
Here, we will discuss the first two methods.
Decimal.Round(Decimal) Method
This method is used to round a decimal value to the nearest integer.
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.
Below programs illustrate the use of
Decimal.Round(Decimal) Method:
Example 1:
Output:
Rounded value is 184467440737096
Example 2: For
OverflowException
Compile-Time Errors:
prog.cs(15,51): error CS0594: Floating-point constant is outside the range of type `decimal'
Round(Decimal, Int32) Method
This method is used to round a Decimal value to a specified number of decimal places.
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.
Return Value: This method returns the decimal number equivalent to
d rounded to decimals number of decimal places.
Exception: This method throws
ArgumentOutOfRangeException if decimals is not a value from 0 to 28.
Below programs illustrate the use of
Decimal.Round(Decimal, Int32) Method:
Example 1:
Output:
Rounded value is 7922816251426433759354.3950
Example 2: For
ArgumentOutOfRangeException