As of the next release Round() now supports a midpoint rounding mode 🙂
The Round() functions on Double, Maths, and Currency now let you choose how exact midpoint values are rounded, via a new RoundingMode enum:
RoundingMode.ToEven — exact midpoints round to the even result (ties-to-even, banker's rounding). This is the default.
RoundingMode.AwayFromZero - exact midpoints round away from zero, on both sides of zero.
Existing calls are completely unchanged: value.Round(), value.Round(2), Maths.Round(value), and Maths.Round(value, 2) all keep their current behaviour.
Print(2.5.Round()) # 2 (ties-to-even, the default)
Print(1.25.Round(1)) # 1.2
Print(1250.0.Round(-2)) # 1200
When you need a different policy, pass the mode explicitly - for whole numbers, for a specific number of decimal places, and for negative decimal places (powers of ten) alike:
Print(2.5.Round(RoundingMode.AwayFromZero)) # 3
Print((-2.5).Round(RoundingMode.AwayFromZero)) # -3
Print(1.25.Round(1, RoundingMode.AwayFromZero)) # 1.3
Print(1250.0.Round(-2, RoundingMode.AwayFromZero)) # 1300
Print(Maths.Round(2.5, RoundingMode.AwayFromZero)) # 3
Print(Maths.Round(1.25, 1, RoundingMode.AwayFromZero)) # 1.3
Print(Currency.Parse("4.50").Round(RoundingMode.AwayFromZero)) # 5
The selected policy applies consistently for positive, zero, and negative decimal-place counts, and NaN, infinities, and signed-zero sign behave exactly as before under either policy.
Passing RoundingMode.AwayFromZero is useful when you need to match external systems that round ties away from zero, while the simple value.Round(...) API keeps its familiar ties-to-even default.