Codectionary / Developer documentation / C#

Decimal Midpoint Rounding

Math.Round accepts a rounding policy. Decimal literals use the m suffix and represent common decimal fractions without binary floating-point approximation. Use a .NET 8+ console project; replace Program.cs with this C# example.

Read the syntax

Math.Round accepts a rounding policy. Decimal literals use the m suffix and represent common decimal fractions without binary floating-point approximation. Use a .NET 8+ console project; replace Program.cs with this C# example.

At the exact midpoint, ToEven chooses the result whose final digit is even. AwayFromZero chooses the result farther from zero.

Syntax

Math.Round(decimalValue, digits, MidpointRounding.ToEven)

Examples

Decimal Midpoint Rounding

Use a .NET 8+ console project; replace Program.cs with this C# example.

using System;
using System.Globalization;
decimal amount = 2.345m;
Console.WriteLine(Math.Round(amount, 2, MidpointRounding.ToEven).ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(amount, 2, MidpointRounding.AwayFromZero).ToString("F2", CultureInfo.InvariantCulture));
  1. Expected output: 2.34 2.35
  2. At the exact midpoint, ToEven chooses the result whose final digit is even. AwayFromZero chooses the result farther from zero.
  3. Specify rounding policy explicitly where domain requirements matter.

Best practices

  • Specify rounding policy explicitly where domain requirements matter.
  • Round at defined boundaries rather than after every intermediate arithmetic step.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading