C# round
last modified November 6, 2026
In this article we show how to round numbers in C#.
Rounding replaces a number with a nearby number that has fewer digits, for example 3.14159 with 3.14. It makes results easier to read and is required in calculations with money.
Rounding methods
The Math class has four rounding methods:
Math.Ceilingreturns the smallest integer greater than or equal to the numberMath.Floorreturns the largest integer less than or equal to the numberMath.Truncateremoves the fractional partMath.Roundrounds to the nearest integer or to a number of decimal places
The numeric types have the same methods: double.Round,
float.Round, decimal.Round, and so on. The
Math.Round overloads for double and
decimal take the number of decimal places and a rounding mode:
Math.Round(decimal d, int decimals, MidpointRounding mode)
The MidpointRounding enumeration has these members:
| Mode | Description |
|---|---|
| ToEven | to the nearest number; a midpoint goes to the even neighbour (default) |
| AwayFromZero | to the nearest number; a midpoint goes away from zero |
| ToZero | towards zero (truncation) |
| ToNegativeInfinity | downwards, like Floor |
| ToPositiveInfinity | upwards, like Ceiling |
A midpoint is a value exactly halfway between two candidates, such as 2.5 between 2 and 3. The first two modes differ only for midpoints. The last three, added in .NET Core 3.0, are directed modes that apply to every value.
Ceiling, Floor, and Truncate
The first example compares the three methods on positive and negative values.
double[] values = [1.467, -1.467, 6.5, -6.5];
Console.WriteLine($"{"value",8}{"Ceiling",9}{"Floor",7}{"Truncate",10}{"(int)",7}");
foreach (var v in values)
{
Console.WriteLine($"{v,8}{Math.Ceiling(v),9}{Math.Floor(v),7}{Math.Truncate(v),10}{(int) v,7}");
}
Console.WriteLine(double.Ceiling(6.967));
Console.WriteLine(decimal.Floor(6.967m));
For positive numbers, Floor and Truncate agree. For
negative numbers, Floor goes down, to -2, while
Truncate goes towards zero, to -1.
{(int) v,7}
A cast to int truncates like Math.Truncate. It does
not round.
Console.WriteLine(double.Ceiling(6.967)); Console.WriteLine(decimal.Floor(6.967m));
The methods are also available on the numeric types.
$ dotnet run
value Ceiling Floor Truncate (int)
1.467 2 1 1 1
-1.467 -1 -2 -1 -1
6.5 7 6 6 6
-6.5 -6 -7 -6 -6
7
6
Rounding to decimal places
The second argument of Math.Round gives the number of decimal
places.
double n1 = 1.23487;
double n2 = 3.97451;
for (int digits = 4; digits >= 0; digits--)
{
Console.WriteLine($"{digits}: {Math.Round(n1, digits),-8} {Math.Round(n2, digits)}");
}
float f = 1.23487f;
Console.WriteLine($"float: {MathF.Round(f, 2)} {float.Round(f, 2)}");
decimal d = 1.23487m;
Console.WriteLine($"decimal: {Math.Round(d, 2)} {decimal.Round(d, 2)}");
We round two numbers to four, three, two, one, and zero places.
Trailing zeros are not printed: 3.97451 rounded to one place is 4.0, which is
displayed as 4. A format string such as F1 shows a
fixed number of decimals.
Console.WriteLine($"float: {MathF.Round(f, 2)} {float.Round(f, 2)}");
For float, we use MathF.Round or
float.Round. Math.Round has no float
overload and would convert the value to double.
$ dotnet run 4: 1.2349 3.9745 3: 1.235 3.975 2: 1.23 3.97 1: 1.2 4 0: 1 4 float: 1.23 1.23 decimal: 1.23 1.23
Midpoint rounding
By default, Math.Round rounds midpoints to the nearest even
number. This is called banker's rounding.
double[] values = [0.5, 1.5, 2.5, 3.5, -2.5, 2.4, 2.6];
Console.WriteLine($"{"value",6}{"ToEven",8}{"AwayFromZero",14}");
foreach (var v in values)
{
Console.WriteLine($"{v,6}{Math.Round(v),8}{Math.Round(v, MidpointRounding.AwayFromZero),14}");
}
Console.WriteLine($"Convert.ToInt32(2.5) = {Convert.ToInt32(2.5)}");
Console.WriteLine($"Convert.ToInt32(3.5) = {Convert.ToInt32(3.5)}");
We round midpoints and two other values with the default mode and with
AwayFromZero.
With ToEven, 0.5 and 2.5 go down, while 1.5 and 3.5 go up. Over
many values, ups and downs balance, so sums of rounded values are not biased
upwards. AwayFromZero is the rounding taught at school. The values
2.4 and 2.6 are not midpoints and round the same in both modes.
Console.WriteLine($"Convert.ToInt32(2.5) = {Convert.ToInt32(2.5)}");
Convert.ToInt32 rounds to even too, unlike a cast, which
truncates.
$ dotnet run value ToEven AwayFromZero 0.5 0 1 1.5 2 2 2.5 2 3 3.5 4 4 -2.5 -2 -3 2.4 2 2 2.6 3 3 Convert.ToInt32(2.5) = 2 Convert.ToInt32(3.5) = 4
All rounding modes
This example rounds several decimal values to two places with
each mode.
decimal[] values = [2.345m, 2.355m, 2.341m, 2.349m, -2.345m, -2.341m];
MidpointRounding[] modes =
[
MidpointRounding.ToEven, MidpointRounding.AwayFromZero,
MidpointRounding.ToZero, MidpointRounding.ToNegativeInfinity,
MidpointRounding.ToPositiveInfinity
];
Console.Write($"{"",20}");
foreach (var v in values)
{
Console.Write($"{v,8}");
}
Console.WriteLine();
foreach (var mode in modes)
{
Console.Write($"{mode,-20}");
foreach (var v in values)
{
Console.Write($"{decimal.Round(v, 2, mode),8}");
}
Console.WriteLine();
}
The values 2.345, 2.355, and -2.345 are midpoints; the others are not.
ToEven and AwayFromZero differ only in the midpoint
columns: 2.345 goes to 2.34 or 2.35. The directed modes ignore the distance:
ToPositiveInfinity turns 2.341 into 2.35, and
ToNegativeInfinity turns -2.341 into -2.35.
$ dotnet run
2.345 2.355 2.341 2.349 -2.345 -2.341
ToEven 2.34 2.36 2.34 2.35 -2.34 -2.34
AwayFromZero 2.35 2.36 2.34 2.35 -2.35 -2.34
ToZero 2.34 2.35 2.34 2.34 -2.34 -2.34
ToNegativeInfinity 2.34 2.35 2.34 2.34 -2.35 -2.35
ToPositiveInfinity 2.35 2.36 2.35 2.35 -2.34 -2.34
Binary floating-point values
A double stores numbers in binary, and most decimal fractions,
such as 1.005, have no exact binary representation. The stored value is a
little above or below the written one, which affects rounding.
double[] doubles = [1.005, 2.675, 1.115];
decimal[] decimals = [1.005m, 2.675m, 1.115m];
for (int i = 0; i < doubles.Length; i++)
{
double d = doubles[i];
Console.WriteLine($"{d}: stored as {d:F20}");
Console.WriteLine($" Math.Round(d, 2, AwayFromZero) = {Math.Round(d, 2, MidpointRounding.AwayFromZero)}");
Console.WriteLine($" d.ToString(\"F2\") = {d:F2}");
Console.WriteLine($" decimal, AwayFromZero = {Math.Round(decimals[i], 2, MidpointRounding.AwayFromZero)}");
}
For each value, we print the stored value with 20 decimals and round it in three ways.
Console.WriteLine($"{d}: stored as {d:F20}");
All three doubles are stored slightly below the midpoint, so strictly speaking they should be rounded down.
ToString("F2") rounds the exact stored value, and gives 1.00,
2.67, and 1.11. Math.Round computes with the scaled value, and
for 2.675 and 1.115 the scaling error pushes the result up. The two methods
can disagree, and neither gives 1.01 for 1.005.
The decimal type stores decimal digits exactly, so the literals
are true midpoints and round as written. Use decimal when exact
decimal rounding matters.
$ dotnet run
1.005: stored as 1.00499999999999989342
Math.Round(d, 2, AwayFromZero) = 1
d.ToString("F2") = 1.00
decimal, AwayFromZero = 1.01
2.675: stored as 2.67499999999999982236
Math.Round(d, 2, AwayFromZero) = 2.68
d.ToString("F2") = 2.67
decimal, AwayFromZero = 2.68
1.115: stored as 1.11499999999999999112
Math.Round(d, 2, AwayFromZero) = 1.12
d.ToString("F2") = 1.11
decimal, AwayFromZero = 1.12
Special values and errors
Math.Round does not throw for special floating-point values.
double[] numbers = [3.14159, double.NaN, double.PositiveInfinity, 1e20 + 0.5];
foreach (var num in numbers)
{
Console.WriteLine($"Math.Round({num}, 2) = {Math.Round(num, 2)}");
}
Console.WriteLine($"Math.Round(-0.4) = {Math.Round(-0.4)}");
try
{
Math.Round(1.5, 16);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
double big = 1e20;
Console.WriteLine($"(int) {big} = {(int) big}");
try
{
Console.WriteLine(checked((int) big));
}
catch (OverflowException e)
{
Console.WriteLine(e.Message);
}
NaN and infinity are returned unchanged. A value as large as 1E+20 has no
fractional digits left in a double, so rounding does nothing.
Console.WriteLine($"Math.Round(-0.4) = {Math.Round(-0.4)}");
Rounding a small negative number gives negative zero, which is printed as
-0. It compares equal to 0.
Math.Round(1.5, 16);
The number of decimal places must be between 0 and 15 for
double (0 and 28 for decimal). Other values throw an
ArgumentOutOfRangeException.
Console.WriteLine($"(int) {big} = {(int) big}");
Converting a rounded value that is too large for int does not
throw by default. Since .NET 9, the conversion saturates to
int.MaxValue. In a checked context, it throws an
OverflowException.
$ dotnet run Math.Round(3.14159, 2) = 3.14 Math.Round(NaN, 2) = NaN Math.Round(∞, 2) = ∞ Math.Round(1E+20, 2) = 1E+20 Math.Round(-0.4) = -0 Rounding digits must be between 0 and 15, inclusive. (Parameter 'digits') (int) 1E+20 = 2147483647 Arithmetic operation resulted in an overflow.
Rounding money
Monetary calculations use the decimal type. Where and when to
round is part of the business rules.
(string Item, decimal Price, int Qty)[] cart =
[
("pen", 0.99m, 3),
("notebook", 2.49m, 2),
("stapler", 7.35m, 1),
];
decimal taxRate = 0.075m;
decimal totalOfRounded = 0;
decimal exactTax = 0;
foreach (var (item, price, qty) in cart)
{
decimal tax = price * qty * taxRate;
decimal rounded = Math.Round(tax, 2, MidpointRounding.AwayFromZero);
totalOfRounded += rounded;
exactTax += tax;
Console.WriteLine($"{item,-9} {price * qty,6:F2} tax {tax,-8} -> {rounded:F2}");
}
Console.WriteLine($"Sum of rounded taxes: {totalOfRounded:F2}");
Console.WriteLine($"Rounded sum of taxes: {Math.Round(exactTax, 2, MidpointRounding.AwayFromZero):F2}");
We compute a 7.5% tax for each item of a cart and round it to cents.
decimal rounded = Math.Round(tax, 2, MidpointRounding.AwayFromZero);
Many tax and invoicing rules require rounding half away from zero, so we pass
the mode explicitly instead of relying on the ToEven default.
The sum of the rounded taxes is 1.14, but rounding the exact sum gives 1.15. Rounding each line and rounding the total are both legitimate, but they can differ by a cent, so a program must follow one rule consistently.
$ dotnet run pen 2.97 tax 0.22275 -> 0.22 notebook 4.98 tax 0.37350 -> 0.37 stapler 7.35 tax 0.55125 -> 0.55 Sum of rounded taxes: 1.14 Rounded sum of taxes: 1.15
Rounding to a multiple
Math.Round rounds to decimal places only. To round to another
step, such as 0.05 or 100, we divide by the step, round, and multiply back.
Console.WriteLine(RoundTo(17.42m, 0.05m));
Console.WriteLine(RoundTo(17.47m, 0.05m));
Console.WriteLine(RoundTo(1234m, 100m));
Console.WriteLine(RoundTo(1250m, 100m));
Console.WriteLine(RoundTo(7.4m, 0.5m));
Console.WriteLine(CeilingTo(17.01m, 0.25m));
static decimal RoundTo(decimal value, decimal step) =>
Math.Round(value / step, MidpointRounding.AwayFromZero) * step;
static decimal CeilingTo(decimal value, decimal step) =>
Math.Ceiling(value / step) * step;
RoundTo rounds to the nearest multiple of the step.
Math.Round(value / step, MidpointRounding.AwayFromZero) * step;
17.42 is closer to 17.40 than to 17.45, and 1250 is a midpoint that goes up
to 1300. With decimal, the division and multiplication are exact
for such steps; with double, the result could be off in the last
digits.
Math.Ceiling(value / step) * step;
CeilingTo always rounds up, for example to the next quarter.
$ dotnet run 17.40 17.45 1200 1300 7.5 17.25
Source
In this article we have rounded numbers in C# with the Math
methods and the rounding modes.
Author
List all C# tutorials.