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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Rounded value is 184467440737096

Example 2: For OverflowException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Rounded value is 7922816251426433759354.3950

Example 2: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.