Skip to main content

How to Round a Number to Two Decimal Places with Math.Round in C#

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Math.Round(value, 2) rounds a number to two decimal places. Watch the halfway values: by default 2.125 becomes 2.12, not 2.13, unless you pass MidpointRounding.AwayFromZero.

Microsoft's reference for the decimal version, Math.Round(Decimal, Int32): Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number. Fractional digits are the digits after the decimal point. A midpoint is a value exactly halfway between two possible results. The reference again: In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3.47500 is a midpoint value if it is to be rounded to two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer.

Syntax​

Syntax
decimal <rounded> = Math.Round(<value>, <decimals>);
decimal <rounded> = Math.Round(<value>, <decimals>, MidpointRounding.<mode>);
PartWhat it is
Math.RoundThe static method of the Math class that rounds a number
<value>The number to round: a decimal here. It also takes a double
<decimals>How many digits to keep after the decimal point: 2 for two decimal places
MidpointRounding.<mode>Optional: what to do with a midpoint, such as AwayFromZero. Without it, a midpoint goes to the even neighbour
<rounded>The rounded number. Math.Round returns it and doesn't change <value>

The Example​

A console app called MathRoundDecimals works out the average of three test scores, 78, 85 and 91, rounds it to two decimal places, and then rounds two midpoints, 2.125 and 2.135, with the default and with MidpointRounding.AwayFromZero.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n MathRoundDecimals
cd MathRoundDecimals
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/MathRoundDecimals/MathRoundDecimals.csproj:
Determining projects to restore...
Restored /Users/you/source/MathRoundDecimals/MathRoundDecimals.csproj (in 42 ms).
Restore succeeded.

dotnet new console -n MathRoundDecimals creates a folder named MathRoundDecimals with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or MathRoundDecimals.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the average:

Program.cs (the average)
// The average of three test scores has a long tail of decimals.
decimal average = (78m + 85m + 91m) / 3;
Console.WriteLine($"Average: {average}");

The m after each number makes it a decimal. The three scores add up to 254, and 254 divided by 3 never ends, so the average has a long tail of decimals.

Then round it:

Program.cs (round to two places)
// Math.Round(value, 2) rounds the number itself to two decimal places.
decimal rounded = Math.Round(average, 2);
Console.WriteLine($"Rounded to 2 places: {rounded}");

Math.Round(average, 2) returns the average rounded to two decimal places, as a new decimal.

Last, the two midpoints:

Program.cs (midpoints)
// Halfway values: by default they go to the even neighbour.
decimal[] halfway = [2.125m, 2.135m];
foreach (decimal value in halfway)
{
decimal toEven = Math.Round(value, 2);
decimal awayFromZero = Math.Round(value, 2, MidpointRounding.AwayFromZero);
Console.WriteLine($"{value}: default {toEven}, AwayFromZero {awayFromZero}");
}

2.125 and 2.135 each sit exactly halfway between two numbers with two decimal places. The loop rounds each one twice: Math.Round(value, 2) uses the default, and Math.Round(value, 2, MidpointRounding.AwayFromZero) names the mode.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Average: 84.66666666666666666666666667
Rounded to 2 places: 84.67
2.125: default 2.12, AwayFromZero 2.13
2.135: default 2.14, AwayFromZero 2.14

macOS-style terminal window titled Terminal — MathRoundDecimals showing dotnet run printing an average of 84.66666666666666666666666667, Rounded to 2 places: 84.67, 2.125 giving 2.12 by default and 2.13 with AwayFromZero, and 2.135 giving 2.14 both ways

The average, 84.66666666666666666666666667, became 84.67. The midpoints are where the modes differ. By default, 2.125 became 2.12 and 2.135 became 2.14: each went to the neighbour whose last digit is even. The reference calls this rounding to nearest even, or banker's rounding: Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. With AwayFromZero, both went up, to 2.13 and 2.14: Midpoint values are rounded to the next number away from zero.

The Complete File​

macOS-style code window titled Program.cs — MathRoundDecimals showing the average of 78, 85 and 91 as a decimal, Math.Round rounding it to two decimal places, and a loop that rounds the midpoints 2.125 and 2.135 with the default and with MidpointRounding.AwayFromZero

Program.cs
// The average of three test scores has a long tail of decimals.
decimal average = (78m + 85m + 91m) / 3;
Console.WriteLine($"Average: {average}");

// Math.Round(value, 2) rounds the number itself to two decimal places.
decimal rounded = Math.Round(average, 2);
Console.WriteLine($"Rounded to 2 places: {rounded}");

// Halfway values: by default they go to the even neighbour.
decimal[] halfway = [2.125m, 2.135m];
foreach (decimal value in halfway)
{
decimal toEven = Math.Round(value, 2);
decimal awayFromZero = Math.Round(value, 2, MidpointRounding.AwayFromZero);
Console.WriteLine($"{value}: default {toEven}, AwayFromZero {awayFromZero}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Expecting 2.125 to round to 2.13Math.Round(2.125m, 2) returned 2.12. The reference: By default, the Round method uses the round to nearest even convention.Name the mode you want: Math.Round(2.125m, 2, MidpointRounding.AwayFromZero) returned 2.13
Calling Math.Round without keeping the resultMath.Round(price, 2); on a line of its own left price at 4.567Assign the result: after price = Math.Round(price, 2);, it printed 4.57
Expecting two decimal places to always print two digitsMath.Round(2.5m, 2) printed 2.5, not 2.50. The reference says it for the version that takes a mode: If d has fewer fractional digits than decimals, d is returned unchanged.Choose the digits when you print: with using System.Globalization;, rounded.ToString("0.00", CultureInfo.InvariantCulture) printed 2.50

FAQ​

How do I always round up, or always round down?​

Use one of the other three modes. They work on every value, not only on midpoints:

Program.cs (up, down and toward zero)
decimal value = 2.121m;
Console.WriteLine($"ToPositiveInfinity: {Math.Round(value, 2, MidpointRounding.ToPositiveInfinity)}");
Console.WriteLine($"ToNegativeInfinity: {Math.Round(value, 2, MidpointRounding.ToNegativeInfinity)}");
Console.WriteLine($"ToZero: {Math.Round(-2.129m, 2, MidpointRounding.ToZero)}");
Output
ToPositiveInfinity: 2.13
ToNegativeInfinity: 2.12
ToZero: -2.12

ToPositiveInfinity took 2.121 up to 2.13, ToNegativeInfinity took it down to 2.12, and ToZero cut -2.129 to -2.12. The reference: In .NET Core 3.0 and later versions, three additional rounding strategies are available through the MidpointRounding enumeration. These strategies are used in all cases, not just for midpoint values as MidpointRounding.ToEven and MidpointRounding.AwayFromZero are.

Should I round a double or a decimal?​

Round a decimal when the digits after the point matter, as with money. A double holds most decimal fractions only approximately:

Program.cs (double and decimal)
using System.Globalization;

double asDouble = 2.675;
decimal asDecimal = 2.675m;

Console.WriteLine($"double 2.675 -> {Math.Round(asDouble, 2)}");
Console.WriteLine($"decimal 2.675 -> {Math.Round(asDecimal, 2)}");
Console.WriteLine($"What the double really holds: {asDouble.ToString("G17", CultureInfo.InvariantCulture)}");
Output
double 2.675 -> 2.68
decimal 2.675 -> 2.68
What the double really holds: 2.6749999999999998

Both rounded to 2.68 on .NET 10, but the double doesn't hold 2.675: it holds 2.6749999999999998, just below the midpoint. .NET 11 changes Math.Round for double to round that exact value, and its breaking-change notice shows the same case: For example, 655.925 is stored as 655.924999999999954525…, which is below the 655.925 midpoint, so the correct result is 655.92. It explains: double and float are binary floating-point types and can't exactly represent most decimal fractions. For exact decimal rounding of decimal quantities, prefer Decimal.

Conclusion​

Math.Round(value, 2) rounds a number to two decimal places and returns the result, so assign it. Midpoints go to the even neighbour unless you pass a MidpointRounding mode: AwayFromZero to take 2.125 up to 2.13, and ToPositiveInfinity or ToNegativeInfinity to always go up or down. Round decimal values when the digits matter, and choose how many digits to show when you print.

Additional Resources​