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. This method provides several overloads to handle different data types and rounding scenarios.

Syntax

The following are the primary syntax forms of 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)

Parameters

  • value ? The decimal or double number to be rounded.

  • digits ? The number of fractional digits in the return value.

  • mode ? Specification for how to round value if it is midway between two other numbers.

Return Value

Returns the number nearest to value that contains a number of fractional digits equal to digits.

Math.Round() Process Input: 3.67 Math.Round() Output: 4 Rounds to the nearest whole number

Using Math.Round() with Decimal Values

Example

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));
   }
}

The output of the above code is −

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

Using Math.Round() with Double Values

Example

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));
   }
}

The output of the above code is −

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

Using Math.Round() with Specified Decimal Places

Example

using System;

public class Demo {
   public static void Main() {
      double value = 123.456789;
      Console.WriteLine("Original value: " + value);
      Console.WriteLine("Rounded to 2 places: " + Math.Round(value, 2));
      Console.WriteLine("Rounded to 4 places: " + Math.Round(value, 4));
      Console.WriteLine("Rounded to 0 places: " + Math.Round(value, 0));
   }
}

The output of the above code is −

Original value: 123.456789
Rounded to 2 places: 123.46
Rounded to 4 places: 123.4568
Rounded to 0 places: 123

Using Math.Round() with MidpointRounding

When a number is exactly halfway between two possible rounded values, you can specify the rounding behavior −

Example

using System;

public class Demo {
   public static void Main() {
      double midpoint = 2.5;
      Console.WriteLine("Value: " + midpoint);
      Console.WriteLine("ToEven: " + Math.Round(midpoint, MidpointRounding.ToEven));
      Console.WriteLine("AwayFromZero: " + Math.Round(midpoint, MidpointRounding.AwayFromZero));
      
      double negative = -2.5;
      Console.WriteLine("Value: " + negative);
      Console.WriteLine("ToEven: " + Math.Round(negative, MidpointRounding.ToEven));
      Console.WriteLine("AwayFromZero: " + Math.Round(negative, MidpointRounding.AwayFromZero));
   }
}

The output of the above code is −

Value: 2.5
ToEven: 2
AwayFromZero: 3
Value: -2.5
ToEven: -2
AwayFromZero: -3

Conclusion

The Math.Round() method is essential for controlling precision in numeric calculations. It supports various data types and provides flexible rounding options including specifying decimal places and handling midpoint values with different rounding strategies.

Updated on: 2026-03-17T07:04:35+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements