What is the rounding rule when the last digit is 5 in .NET?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In .NET, a value ending in 5 at the rounding position does not always round upward. By default, midpoint values are rounded using ToEven, also called banker's rounding, which means the result goes to the nearest even number rather than always away from zero.
The Default Midpoint Rule
The default behavior of Math.Round uses midpoint rounding to even. That affects numbers that are exactly halfway between two representable rounded results.
Why those results? Because:
- '
2.5is halfway between2and3, and2is even' - '
3.5is halfway between3and4, and4is even'
This rule reduces cumulative bias in repeated rounding operations, which is why it is a common default in financial and statistical systems.
Use AwayFromZero If You Want Traditional Half-Up Behavior
If your business rule says 2.5 should become 3 and -2.5 should become -3, specify MidpointRounding.AwayFromZero.
This is often the behavior people expect from school arithmetic, but it is not the default in .NET.
Precision Argument Changes Which Digit Matters
When you round to a specific number of decimal places, the same midpoint rule applies at that digit.
At one decimal place:
- '
2.25is halfway between2.2and2.3, so it goes to2.2' - '
2.35is halfway between2.3and2.4, so it goes to2.4'
Again, the even final digit wins when the value is exactly at the midpoint.
Be Careful with double
Not every number that looks like a midpoint in decimal is stored exactly as a midpoint in binary floating-point. That matters especially with double.
Developers are often surprised by cases like this because the internal binary representation may not be exactly 2.675, so the number is not treated as a perfect midpoint when rounding happens.
If decimal precision is important, use decimal rather than double.
That does not change the midpoint rule itself, but it avoids many binary floating-point surprises.
Pick the Rule That Matches the Domain
There is no universally correct rounding rule for all applications. What matters is choosing the one your domain expects and using it explicitly.
For example:
- statistical aggregation often prefers
ToEven - UI display or business rules may prefer
AwayFromZero - currency calculations often need
decimalplus an explicit midpoint mode
If different parts of the codebase silently assume different rules, bugs become very hard to track down.
Common Pitfalls
One common mistake is assuming that any trailing 5 always rounds upward. In .NET, that is not true unless you explicitly request a different midpoint mode.
Another is blaming the rounding rule when the real problem is floating-point representation. A double that looks like a midpoint in source code may not be an exact midpoint internally.
Developers also sometimes rely on defaults in one place and specify AwayFromZero elsewhere. If the application has rounding requirements, make the choice explicit and consistent.
Finally, remember that rounding and formatting are not the same. Displaying a number with two decimal places does not necessarily imply the same rules as numerical rounding in business logic.
Summary
- The default .NET midpoint rule is
ToEven, not always-upward rounding. - A value ending in
5rounds to the nearest even result when it is exactly at the midpoint. - Use
MidpointRounding.AwayFromZeroif your domain expects half-up style behavior. - Prefer
decimaloverdoublewhen exact decimal rounding matters. - Make the rounding rule explicit when correctness depends on it.

