C# | Math.Round() Method | Set – 1

In C#, Math.Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method can be overloaded by changing the number and type of the arguments passed. There are total 8 methods in the overload list of the Math.Round() method. Here we will discuss only 4 methods and remaining 4 methods are discussed in C# | Math.Round() Method | Set – 2.

Math.Round(Double)

This method rounds a double-precision floating-point value to the nearest integer value.

Syntax:



public static double Round(double x)

Parameter:

x: A double floating-point number which is to be rounded. Type of this parameter is System.Double.

Return Type:It returns the integer nearest to x and return type is System.Double.

Note: In case if the fractional component of x is halfway between two integers, one of which is even and the other odd, then the even number is returned.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Math.Round(Double) method
using System;
  
class Geeks {
  
    // Main method
    static void Main(string[] args)
    {
  
        // Case-1
        // A double value whose fractional part is 
        // less than the halfway between two 
        // consecutive integers
        Double dx1 = 12.434565d;
  
        // Output value will be 12
        Console.WriteLine("Rounded value of " + dx1 + 
                          " is " + Math.Round(dx1));
  
        // Case-2
        // A double value whose fractional part is 
        // greater than the halfway between two 
        // consecutive integers
        Double dx2 = 12.634565d;
  
        // Output value will be 13
        Console.WriteLine("Rounded value of " + dx2 + 
                          " is " + Math.Round(dx2));
    }
}

chevron_right


Output:

Rounded value of 12.434565 is 12
Rounded value of 12.634565 is 13

Explanation: In above code suppose the user wants to round off the above specified double value to nearest integer. So the compiler will first check whether that double value is greater than or less than the even and odd integral value of that double number. If it is less than the halfway value, its output will be floor value, else if greater than halfway value, its output will be the ceiling value.


Math.Round(Double, Int32)

This method rounds a double precision floating-point value to a specified number of fractional digits.

Syntax:



public static double Round(double x, Int32 y)

Parameter:

x: A double floating-point number which is to be rounded. Type of this parameter is System.Double.
y: It is the number of fractional digits in the returned value. Type of this parameter is System.Int32.

Return Type:It returns the integer nearest to x which contains a number of fractional digits equal to y and return type is System.Double.

Exception: This method will give ArgumentOutOfRangeException if the value of y is less than 0 or greater than 15.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Math.Round(Double, Int32) method
using System;
   
class Geeks {
   
    // Main method
    static void Main(string[] args)
    {
   
        // double type
        Double dx1 = 12.434565d;
   
        // using method
        Console.WriteLine("Rounded value of " + dx1 + 
                          " is " + Math.Round(dx1, 4));
   
        // double type
        Double dx2 = 12.634565d;
   
        // using method
        Console.WriteLine("Rounded value of " + dx2 + 
                          " is " + Math.Round(dx2,2));
    }
}

chevron_right


Output:

Rounded value of 12.434565 is 12.4346
Rounded value of 12.634565 is 12.63

Explanation: Method will check that whether the digit next to the specified number of decimal digits is greater than or equals to 5 or not. If it is greater than or equal to 5 then it increments the previous number else previous digit remains the same.

Math.Round(Decimal)

This method rounds off a decimal value whose precision is 128 bits to the nearest integer value.

Syntax:

public static decimal Round(decimal x)

Parameter:



x: It is decimal number which is to be rounded. Type of this parameter is System.Decimal.

Return Type:It returns the integer nearest to x and return type is System.Decimal.

Note: In case if the fractional component of x is halfway between two integers, one of which is even and the other odd, then the even number is returned.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Math.Round(Decimal) method
using System;
  
class Geeks {
  
    // Main method
    static void Main(string[] args)
    {
  
        // Case-1
        // A decimal value whose fractional part is 
        // less than the halfway between two 
        // consecutive integers
        Decimal dec1 = 12.345m;
  
        Console.WriteLine("Value of dec1 is " + dec1);
  
        Console.WriteLine("Rounded value of " + dec1 + 
                          " is " + Math.Round(dec1));
  
        // Case-2
        // A double value whose fractional part is 
        // greater than the halfway between two 
        // consecutive integers
        Decimal dec2 = 12.785m;
  
        Console.WriteLine("Value of dec2 is " + dec2);
  
        Console.WriteLine("Rounded value of " + dec2 + 
                          " is " + Math.Round(dec2));
    }
}

chevron_right


Output:

Value of dec1 is 12.345
Rounded value of 12.345 is 12
Value of dec2 is 12.785
Rounded value of 12.785 is 13

Math.Round(Decimal, Int32)

This method rounds a decimal value to a specified number of fractional digits.

Syntax:

public static decimal Round(decimal x, Int32 y)

Parameter:

x: A decimal number which is to be rounded. Type of this parameter is System.Decimal.
y: It is the number of fractional digits in the returned value. Type of this parameter is System.Int32.

Return Type:It returns the integer nearest to x which contains a number of fractional digits equal to y and return type is System.Decimal.



Exception: This method will give ArgumentOutOfRangeException if the value of y is less than 0 or greater than 15 and OverflowException in case of result is outside the range of a Decimal.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// Math.Round(Decimal, Int32) method
using System;
  
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Case - 1
        // The value of digit after specified 
        // number is less than 5 & Here y = 3
        Decimal dx1 = 12.2234565m;
  
        // Output value will be 12.223
        Console.WriteLine("Rounded value of " + dx1 + 
                          " is " + Math.Round(dx1, 3));
  
        // Case - 2
        // The value of digit after specified 
        // number is greater than 5 & Here y = 4
        Decimal dx2 = 12.8734765m;
  
        // Output value will be 12.8735
        Console.WriteLine("Rounded value of " + dx2 + 
                          " is " + Math.Round(dx2, 4));
  
    }
}

chevron_right


Output:

Rounded value of 12.2234565 is 12.223
Rounded value of 12.8734765 is 12.8735

Note: The above codes for Decimal Value works in a similar way as in case of Double value. The only difference between Decimal type and Double type lies within the concept of precision i.e. in case of Double, precision is 64-bit and in case of Decimal precision is 128-bits.

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=netframework-4.7.2



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 :

1


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