MathF.Round() Method in C# with Examples | Set – 2

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:

  1. MathF.Round(Single) Method
  2. MathF.Round(Single, Int32) Method
  3. MathF.Round(Single, Int32, MidpointRounding) Method
  4. 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:


filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// MathF.Round(Single, Int32,
// MidpointRounding) method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
        // The 4 values are store in an float
        // type array name 'val'
        float[] x = { 8.115f, 7.135f, 9.165f, 6.175f };
  
        Console.WriteLine("Rounded values are:");
  
        // 'foreach' loop iterates through
        // each item from the array 'values'
        // and storing the items in a new
        // variable 'x'
        foreach(float value in x)
  
            // '{0}' specify the variable 'x' which is
            // in 'foreach' loop and '{1}' specify the
            // rounded value, here '2' defines the number
            // of digit after point, e.g. 4.135f == 4.14f,
            // after '4f.' there is 2 digits'.14f'
            // and here '.ToEven' select the nearest even
            // number e.g 4.125f == 4.12f, here nearest even
            // number is '12f',
            Console.WriteLine("{0} == {1}", value, MathF.Round(value, 2,
                                              MidpointRounding.ToEven));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// MathF.Round(Single, MidpointRounding) 
// method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // 'x' is float type variable
        // which holds the value 7.1f
        float x = 7.1f;
  
        Console.WriteLine("Inside Loop:\n");
  
        //'for loop', it execute the next
        // output for 8 times
        for (int i = 0; i <= 8; i++) {
  
            // '{0}' specify the variable 'x' and
            // '{1}' specify the rounded value
            Console.WriteLine("{0} = {1}", x, MathF.Round(x,
                            MidpointRounding.AwayFromZero));
  
            // increment 'x' by '0.1'
            x += 0.1f;
        }
  
        // a new value is assigned
        // to variable 'x'
        x = 4.5f;
  
        // prints a new line
        Console.WriteLine();
  
        //'{0}'specify the variable 'x' in which a new
        // value 4.5f is assigned and '{1}' specify the
        // new rounded value
        Console.WriteLine("Outside Loop : {0} = {1}", x,
          MathF.Round(x, MidpointRounding.AwayFromZero));
    }
}

chevron_right


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


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.