In C#, Math.Round(value) rounds a number to the nearest whole number. Math.Round(3.7) is 4 and Math.Round(3.2) is 3. With a second number, Math.Round(value, digits) rounds to that many decimal places.
There is one surprise. When a number is exactly halfway, such as 2.5, C# does not always round up. By default it rounds to the nearest even number, so Math.Round(2.5) is 2 and Math.Round(3.5) is 4. This is banker's rounding. If you want the school rule (halves go up), pass MidpointRounding.AwayFromZero. Other rounding styles are Floor(), Ceiling(), and Truncate().
This is a static member of the static class System.Math. You call it with the class name, such as Math.Round(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Round has been in .NET from the start. The modes ToZero, ToNegativeInfinity, and ToPositiveInfinity need .NET Core 3.0+.
Syntax
Pass a number. Add a digits count to keep decimals, and a MidpointRounding value to choose what happens at exactly halfway.
C#
double a = Math.Round(3.7); // 4
double b = Math.Round(2.5); // 2 (banker's rounding)
double c = Math.Round(2.5, MidpointRounding.AwayFromZero); // 3
double d = Math.Round(3.14159, 2); // 3.14
decimal e = Math.Round(2.675m, 2); // 2.68
Call
What it does
Math.Round(3.7)
Nearest whole number: 4.
Math.Round(2.5)
Exactly halfway, so it goes to the even neighbour: 2.
Math.Round(2.5, MidpointRounding.AwayFromZero)
Halves move away from zero: 3.
Math.Round(3.14159, 2)
Keeps two decimals: 3.14.
digits must be from 0 to 15 for a double and from 0 to 28 for a decimal, or ArgumentOutOfRangeException is thrown. The double version returns a double, so cast for an int: (int)Math.Round(x). Remember that a double such as 2.675 is stored a hair below 2.675, so it is not an exact half. Use decimal for money.
Library declarations
C#
public static double Round(double value);
public static double Round(double value, int digits);
public static double Round(double value, MidpointRounding mode);
public static double Round(double value, int digits, MidpointRounding mode);
public static decimal Round(decimal d);
public static decimal Round(decimal d, int decimals);
public static decimal Round(decimal d, MidpointRounding mode);
public static decimal Round(decimal d, int decimals, MidpointRounding mode);
Parameters
Parameter
Description
value / d
Required. The double or decimal number to round.
digits / decimals
Optional. How many decimal places to keep. From 0 to 15 for double, 0 to 28 for decimal. The default is 0 (a whole number).
mode
Optional. A MidpointRounding value for exact halves: ToEven (default), AwayFromZero, ToZero, ToNegativeInfinity, or ToPositiveInfinity.
Return Value
Return Value
Description
double or decimal
The rounded number, in the same type as the input. NaN and infinity are returned unchanged.
C# Examples
Example 1 — Halves Go to the Even Number
Round numbers that are not halfway and numbers that are exactly halfway, and notice which way each goes.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Round(2.6));
Console.WriteLine(Math.Round(2.4));
Console.WriteLine(Math.Round(2.5));
Console.WriteLine(Math.Round(3.5));
Console.WriteLine(Math.Round(-2.5));
}
}
Output
3
2
2
4
-2
How It Works
Not halfway. 2.6 goes to 3 and 2.4 goes to 2, as you expect. The default rule only matters at exactly .5.
2.5 goes down. It is exactly between 2 and 3. The default picks the even neighbour, and 2 is even, so the answer is 2.
3.5 goes up. The neighbours are 3 and 4. Now 4 is the even one, so the answer is 4.
Negatives follow the same rule. -2.5 sits between -3 and -2. The even neighbour is -2, so you get -2.
Example 2 — Choose How Halves Round
Pass a MidpointRounding value to control what happens at exactly halfway. The last three modes need .NET Core 3.0 or later.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Round(2.5, MidpointRounding.AwayFromZero));
Console.WriteLine(Math.Round(-2.5, MidpointRounding.AwayFromZero));
Console.WriteLine(Math.Round(2.5, MidpointRounding.ToZero));
Console.WriteLine(Math.Round(2.5, MidpointRounding.ToPositiveInfinity));
Console.WriteLine(Math.Round(2.5, MidpointRounding.ToNegativeInfinity));
Console.WriteLine(Math.Round(-2.5, MidpointRounding.ToNegativeInfinity));
}
}
Output
3
-3
2
3
2
-3
How It Works
AwayFromZero. This is the school rule: halves move away from zero, so 2.5 becomes 3 and -2.5 becomes -3.
ToZero. Halves move toward zero, so 2.5 becomes 2.
ToPositiveInfinity. Halves always move up on the number line, so 2.5 becomes 3.
ToNegativeInfinity. Halves always move down: 2.5 becomes 2 and -2.5 becomes -3. Only exact halves are affected.
Example 3 — Round to Decimal Places and Money
Keep a chosen number of decimals with a double, then round decimal money values with different modes.
C#
using System;
using System.Globalization;
class Program {
static string Show(decimal value) {
return value.ToString(CultureInfo.InvariantCulture);
}
static void Main() {
double pi = 3.14159;
Console.WriteLine(Math.Round(pi, 3).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(pi, 2).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(pi, 0).ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Show(Math.Round(2.675m, 2)));
Console.WriteLine(Show(Math.Round(2.665m, 2)));
Console.WriteLine(Show(Math.Round(2.665m, 2, MidpointRounding.AwayFromZero)));
}
}
Output
3.142
3.14
3
2.68
2.66
2.67
How It Works
Digits argument.Math.Round(pi, 3) keeps three decimals, 3.142 (the next digit, 5, rounds the 1 up). With 2 decimals you get 3.14, and with 0 you get a whole number, 3.
Plain output.InvariantCulture always prints a dot, never 3,14.
decimal is exact.2.675m really is 2.675, so it is exactly halfway. The default rounds to even: the digit 7 is odd, so it goes up to 8 and the result is 2.68.
Even stays.2.665m is also halfway, but 6 is already even, so the default keeps it: 2.66. With AwayFromZero, the same value becomes 2.67.
Frequently Asked Questions
By default, Math.Round uses banker's rounding (MidpointRounding.ToEven). A number exactly halfway goes to the nearest even whole number, so 2.5 becomes 2 and 3.5 becomes 4.
Pass MidpointRounding.AwayFromZero, as in Math.Round(2.5, MidpointRounding.AwayFromZero). Then -2.5 becomes -3.
Use Math.Round(value, 2). For money, prefer decimal. To only display two decimals, use value.ToString("F2").
15 for double and 28 for decimal. A value outside 0 to those limits throws ArgumentOutOfRangeException.
No. Math is a static class, so you call Math.Round(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.