C# Reference ยท Numbers and Math
Math.Round() method
Reference for Math.Round in C#: banker's rounding, MidpointRounding modes, rounding to decimal places, decimal vs double, and money pitfalls.
Math.Round rounds a number to the nearest integer or to a given number of decimal places. The surprise for newcomers is the default for values exactly halfway between two candidates: .NET rounds them to the nearest even number (also called banker's rounding), so Math.Round(2.5) is 2 and Math.Round(3.5) is 4. This avoids a systematic upward bias when you round many values.
You choose another rule with the MidpointRounding enum. AwayFromZero is the "school" rule (2.5 becomes 3), and ToZero, ToNegativeInfinity and ToPositiveInfinity give directed rounding.
Math.Round(2.5); // 2 Math.Round(2.5, MidpointRounding.AwayFromZero); // 3 Math.Round(3.14159, 2); // 3.14
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
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);
| Name | Description |
|---|---|
value / d |
The number to round. |
digits / decimals |
Digits after the decimal point to keep: 0 to 15 for double, 0 to 28 for decimal. |
mode |
ToEven (default), AwayFromZero, ToZero, ToNegativeInfinity or ToPositiveInfinity. |
Returns: the rounded value, as double or decimal to match the argument. The result is never an integer type, so cast ((int)Math.Round(x)) when you need one.
Errors / undefined behavior: ArgumentOutOfRangeException when digits is outside its allowed range or mode is not a defined value. NaN and infinities are returned unchanged. Casting a rounded double to int is a separate step and misbehaves when the value is out of range.
Usage notes
- The default is not "round half up". If you need 2.5 to become 3 (prices, grades, anything users expect to match a calculator), pass
MidpointRounding.AwayFromZero. - Doubles are not exact.
1.005is stored as 1.0049999999999999, soMath.Round(1.005, 2, MidpointRounding.AwayFromZero)gives1, not1.01: the midpoint never existed in binary. Usedecimalfor money. - Use
decimalfor currency.Math.Round(1.005m, 2, MidpointRounding.AwayFromZero)is1.01, because1.005mis exact. - Rounding a double does not guarantee clean printing. The result is the closest double, which may print with long tails in rare cases. Format with
ToString("F2", CultureInfo.InvariantCulture)for display. - Formatting is not rounding.
value.ToString("F0")only changes text; see Number formatting. - Other rounding helpers.
Math.Floor()andCeiling()round toward an infinity,Math.Truncatetoward zero, andMath.Round(x / step) * steprounds to a multiple such as 0.05. - Negative zero. Rounding
-0.5gives negative zero, which the first example prints as-0. It compares equal to0. - Casting truncates.
(int)2.9is2, not3. Round first when you want the nearest integer. - Integers. For integer division with rounding, add half the divisor before dividing instead of going through
double.