Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Round a number to the nearest even number in C#
The MidpointRounding.ToEven option is used with rounding methods in C# to round a number to the nearest even number when the fractional part is exactly 0.5. This is also known as banker's rounding or round half to even.
Syntax
Following is the syntax for rounding to the nearest even number −
decimal.Round(value, digits, MidpointRounding.ToEven) Math.Round(value, digits, MidpointRounding.ToEven)
Parameters
-
value − The decimal or double number to be rounded
-
digits − The number of decimal places in the return value
-
MidpointRounding.ToEven − Rounds to the nearest even number when the number is halfway between two others
How It Works
When using MidpointRounding.ToEven, the rounding behavior follows these rules:
-
If the fractional part is less than 0.5, round down
-
If the fractional part is greater than 0.5, round up
-
If the fractional part is exactly 0.5, round to the nearest even number
Using decimal.Round() with ToEven
Example
using System;
class Demo {
static void Main() {
decimal val1 = 25.55M;
decimal val2 = 2.5M;
decimal val3 = 3.5M;
decimal val4 = 4.5M;
Console.WriteLine("Original: " + val1 + " Rounded: " + decimal.Round(val1, 0, MidpointRounding.ToEven));
Console.WriteLine("Original: " + val2 + " Rounded: " + decimal.Round(val2, 0, MidpointRounding.ToEven));
Console.WriteLine("Original: " + val3 + " Rounded: " + decimal.Round(val3, 0, MidpointRounding.ToEven));
Console.WriteLine("Original: " + val4 + " Rounded: " + decimal.Round(val4, 0, MidpointRounding.ToEven));
}
}
The output of the above code is −
Original: 25.55 Rounded: 26 Original: 2.5 Rounded: 2 Original: 3.5 Rounded: 4 Original: 4.5 Rounded: 4
Using Math.Round() with ToEven
Example
using System;
class Program {
static void Main() {
double[] values = { 1.5, 2.5, 3.5, 4.5, 5.5 };
Console.WriteLine("Value\tToEven\tAwayFromZero");
Console.WriteLine("-----\t------\t-----------");
foreach (double value in values) {
double toEven = Math.Round(value, 0, MidpointRounding.ToEven);
double awayFromZero = Math.Round(value, 0, MidpointRounding.AwayFromZero);
Console.WriteLine(value + "\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
Comparison of Rounding Methods
| MidpointRounding Option | Behavior | Example: 2.5 ? ? |
|---|---|---|
| ToEven | Round to nearest even number | 2.5 ? 2 |
| AwayFromZero | Round away from zero | 2.5 ? 3 |
| ToZero | Round toward zero (truncate) | 2.5 ? 2 |
Conclusion
MidpointRounding.ToEven provides banker's rounding behavior, which reduces bias in statistical calculations by rounding midpoint values to the nearest even number. This method is particularly useful in financial and scientific applications where rounding bias needs to be minimized.
