C# ToEven property

The ToEven property is a value in the MidpointRounding enumeration that implements banker's rounding. When a number falls exactly between two integers, it rounds to the nearest even number. This method reduces bias in calculations compared to always rounding up or down.

Syntax

Following is the syntax for using MidpointRounding.ToEven

decimal.Round(value, digits, MidpointRounding.ToEven)
Math.Round(value, digits, MidpointRounding.ToEven)

Parameters

  • value − The number to be rounded
  • digits − Number of decimal places in the return value
  • MidpointRounding.ToEven − Rounds to the nearest even number when the value is exactly halfway between two numbers

How Banker's Rounding Works

MidpointRounding.ToEven Behavior 2.5 ? 2 Rounds to nearest even number 3.5 ? 4 Rounds to nearest even number 2.4 ? 2 Normal rounding (not midpoint) Only applies when value is exactly halfway (.5) Reduces statistical bias in large datasets

Example with Different Midpoint Values

using System;

class Demo {
    static void Main() {
        decimal[] values = {1.5M, 2.5M, 3.5M, 4.5M, 5.5M, 6.5M};
        
        Console.WriteLine("Value\tToEven\tAwayFromZero");
        Console.WriteLine("-----\t------\t-----------");
        
        foreach (decimal val in values) {
            decimal toEven = decimal.Round(val, 0, MidpointRounding.ToEven);
            decimal awayFromZero = decimal.Round(val, 0, MidpointRounding.AwayFromZero);
            Console.WriteLine($"{val}\t{toEven}\t{awayFromZero}");
        }
    }
}

The output of the above code is −

Value   ToEven  AwayFromZero
-----   ------  -----------
1.5     2       2
2.5     2       3
3.5     4       4
4.5     4       5
5.5     6       6
6.5     6       7

Example with Decimal Precision

using System;

class Demo {
    static void Main() {
        decimal val1 = 70.45M;
        decimal val2 = 123.235M;
        decimal val3 = 99.975M;
        
        Console.WriteLine($"Original: {val1} ? Rounded: {decimal.Round(val1, 1, MidpointRounding.ToEven)}");
        Console.WriteLine($"Original: {val2} ? Rounded: {decimal.Round(val2, 2, MidpointRounding.ToEven)}");
        Console.WriteLine($"Original: {val3} ? Rounded: {decimal.Round(val3, 1, MidpointRounding.ToEven)}");
    }
}

The output of the above code is −

Original: 70.45 ? Rounded: 70.4
Original: 123.235 ? Rounded: 123.24
Original: 99.975 ? Rounded: 100.0

Comparison with Other Rounding Methods

Value ToEven AwayFromZero ToZero
2.5 2 3 2
3.5 4 4 3
-2.5 -2 -3 -2

Conclusion

MidpointRounding.ToEven implements banker's rounding, which rounds halfway values to the nearest even number. This approach minimizes cumulative rounding errors in financial and statistical calculations, making it the preferred method for reducing bias in large datasets.

Updated on: 2026-03-17T07:04:35+05:30

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements