# Decimal.Round() Method in C# | Set – 1

• Last Updated : 20 Mar, 2019

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:

 // C# program to demonstrate the// Decimal.Round(Decimal) Methodusing 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);        }    }}

Output:

Rounded value is 184467440737096

Example 2: For OverflowException

 // C# program to demonstrate the// Decimal.Round(Decimal) Methodusing 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 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:

 // C# program to demonstrate the// Decimal.Round(Decimal) Methodusing 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);        }    }}

Output:

Rounded value is 7922816251426433759354.3950

Example 2: For ArgumentOutOfRangeException

 // C# program to demonstrate the// Decimal.Round(Decimal) Methodusing 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);        }    }}

Output:

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

Reference:

My Personal Notes arrow_drop_up