MathF.Round() Method in C# with Examples


The MathF.Round() method in C# is used to round a value to the nearest integer or to the particular number of fractional digits.

Syntax

Following is the syntax −

public static float Round (float x);
public static float Round (float x, int digits);
public static float Round (float x, int digits, MidpointRounding mode);
public static float Round (float x, MidpointRounding mode);

Example

Let us now see an example to implement the MathF.Round() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 15.20f;
      float val2 = 3.10f;
      Console.WriteLine("Rounded (val1) = "+MathF.Round(val1));
      Console.WriteLine("Rounded (val2) = "+MathF.Round(val2));
   }
}

Output

This will produce the following output −

Rounded (val1) = 15
Rounded (val2) = 3

Example

Let us now see another example to implement the MathF.Round() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 10.23898f;
      float val2 = 20.878788f;
      Console.WriteLine("Rounded (val1) = "+MathF.Round(val1,2));
      Console.WriteLine("Rounded (val2) = "+MathF.Round(val2,3));
   }
}

Output

This will produce the following output −

Rounded (val1) = 10.24
Rounded (val2) = 20.879

Updated on: 12-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements