Math.Round rounds halves to the nearest even number, as no MidpointRounding is given

tx0/csharp:midpoint-rounding-defaultstrictwarningreliability/arithmeticdepth fastbug

What it finds

Math.Round(2.5) is 2 and Math.Round(3.5) is 4: by default .NET rounds halves to even, which avoids bias in statistics but surprises in invoices and user-facing values. Passing MidpointRounding.AwayFromZero or another mode states which rule applies.

From the rule's source, midpoint_rounding_default.rs.

Example from the rule's tests
using System; class Invoice{    decimal Total(decimal net) => Math.Round(net * 1.19m, 2);tx0 reports: Math.Round rounds halves to even here     decimal Explicit(decimal net) => Math.Round(net, 2, MidpointRounding.AwayFromZero);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:midpoint-rounding-default" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:midpoint-rounding-default] reason="..."

tx0 explain tx0/csharp:midpoint-rounding-default shows the same in the terminal. Found a false positive? Report it.