MathF.Round() Method in C# with Examples | Set – 2
Last Updated :
04 Apr, 2019
In C#, MathF.Round() is a MathF class method which is used to round a value to the nearest integer or to the particular number of fractional digits. It returns the nearest value of the number with the precision equal to the second parameter passed. If the value to round is exactly halfway between an even and an odd number, then the even number is returned. MathF.Round applies IEEE Standard 754, section 4. This method can be overloaded by changing the number and type of arguments passed. There are 4 methods in the overload list of the MathF.Round() method as follows:
- MathF.Round(Single) Method
- MathF.Round(Single, Int32) Method
- MathF.Round(Single, Int32, MidpointRounding) Method
- MathF.Round(Single, MidpointRounding) Method
Here, we will discuss the last two methods.
Math.Round(Single, Int32, MidpointRounding) Method
This method is used to rounds a single precision floating-point value to a specified number of fractional digits. A parameter specifies how to round the value if it is midway between two numbers.
Syntax: public static float Round (float x, int digits, MidpointRounding mode);
Parameters:
x: It is the required single-precision floating-point number which is to be rounded and type of this parameter is System.Single.
digits: It is the number of fractional digits in the return value and type of this parameter is System.Int32.
mode: Specification for how to round x if it is midway between two other numbers and works as MidpointRounding.
Return Type: This method returns the number nearest to x that has a number of fractional digits equal to digits. If the x has fewer fractional digits than digits, x is returned unchanged. Return type of this method is System.Single.
Exceptions:
- ArgumentOutOfRangeException: If the digit is less than 0 or greater than 15.
- ArgumentException: If the mode is not a valid value of MidpointRounding.
Example:
using System;
class Geeks {
public static void Main()
{
float [] x = { 8.115f, 7.135f, 9.165f, 6.175f };
Console.WriteLine( "Rounded values are:" );
foreach ( float value in x)
Console.WriteLine( "{0} == {1}" , value, MathF.Round(value, 2,
MidpointRounding.ToEven));
}
}
|
Output:
Rounded values are:
8.115 == 8.12
7.135 == 7.14
9.165 == 9.16
6.175 == 6.18
MathF.Round(Single, MidpointRounding) Method
This method is used to rounds a single precision floating-point value to the nearest integer. A parameter specifies how to round the value if it is midway between two numbers.
Syntax: public static float Round (float x, MidpointRounding mode);
Parameters:
x: It is the required single-precision floating-point number which is to be rounded and type of this parameter is System.Single.
mode: Specification for how to round x if it is midway between two other numbers and works as MidpointRounding.
Return Type: This method returns the integer nearest value. If the x is halfway between two integers, one of which is even and the other odd, then mode determines which of the two is returned. The return type of this method is System.Single.
Exception: This method gives ArgumentException if the mode is not a valid value of MidpointRounding.
Example:
using System;
class Geeks {
public static void Main()
{
float x = 7.1f;
Console.WriteLine( "Inside Loop:\n" );
for ( int i = 0; i <= 8; i++) {
Console.WriteLine( "{0} = {1}" , x, MathF.Round(x,
MidpointRounding.AwayFromZero));
x += 0.1f;
}
x = 4.5f;
Console.WriteLine();
Console.WriteLine( "Outside Loop : {0} = {1}" , x,
MathF.Round(x, MidpointRounding.AwayFromZero));
}
}
|
Output:
Inside Loop:
7.1 = 7
7.2 = 7
7.3 = 7
7.4 = 7
7.5 = 7
7.599999 = 8
7.699999 = 8
7.799999 = 8
7.899999 = 8
Outside Loop : 4.5 = 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...