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:
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:
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:
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: