Math.Round() Method in C#


The Math.Round() method in C# rounds a value to the nearest integer or to the specified number of fractional digits.

Methods

The following are the methods overloaded by Math.Round() −

Math.Round(Double)
Math.Round(Double, Int32)
Math.Round(Double, Int32, MidpointRounding)
Math.Round(Double, MidpointRounding)
Math.Round(Decimal)
Math.Round(Decimal, Int32)
Math.Round(Decimal, Int32, MidpointRounding)
Math.Round(Decimal, MidpointRounding)

Example

Let us now see an example to implement Math.Round() method i.e. Math.Round(Decimal) −

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = 78.12m;
      Decimal val2 = 30.675m;
      Console.WriteLine("Decimal Value = " + val1);
      Console.WriteLine("Rounded value = " + Math.Round(val1));
      Console.WriteLine("Decimal Value = " + val2);
      Console.WriteLine("Rounded value = " + Math.Round(val2));
   }
}

Output

This will produce the following output −

Decimal Value = 78.12
Rounded value = 78
Decimal Value = 30.675
Rounded value = 31

Example

Let us see another example to implement Math.Round() method i.e. Math.Round(Double) −

using System;
public class Demo {
   public static void Main(){
      Double val1 = 23.10;
      Double val2 = 90.98;
      Console.WriteLine("Double Value = " + val1);
      Console.WriteLine("Rounded value = " + Math.Round(val1));
      Console.WriteLine("Double Value = " + val2);
      Console.WriteLine("Rounded value = " + Math.Round(val2));
   }
}

Output

This will produce the following output −

Double Value = 23.1
Rounded value = 23
Double Value = 90.98
Rounded value = 91

Updated on: 08-Nov-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements