Friday, October 23, 2009

C# Math.Round Banker's or Mathematical Method

I wasn't even aware that there was a banker's method to rounding numbers until I came across what I though was a bug in the Math.Round() function in the .NET library today.

If you call this function:

double x = Math.Round(.005, 2);

then you will discover that x is set to zero. If you call this:

double x = Math.Round(.0051, 2);

then x will be set to 0.01.

I was always taught that you round up from 5. This is still correct and is known as the mathematical method of rounding. However I have recently learned that there's another method and if the least-significant-digit is 5 and you're using the banker's method then you round down instead of up. This is the method that Math.Round() uses by default.

If you want to use the expected mathematical method for the round function then you need to add another parameter to the overloaded Round() function which specifies how you want it to handle midpoint rounding. AwayFromZero will do the trick:

double x = Math.Round(.005, 2, MidpointRounding.AwayFromZero);

This will cause x to be set to .001 as you would expect. The other value defined in the MidpointRounding enum is ToEven and this will do the default of round the midpoint down to zero.

Mystery solved!

1 Sep 2010, just discovered this:

Banker's Rounding

When you add rounded values together, always rounding .5 in the same direction results in a bias that grows with the more numbers you add together. One way to minimize the bias is with banker's rounding.

Banker's rounding rounds .5 up sometimes and down sometimes. The convention is to round to the nearest even number, so that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. Banker's rounding is symmetric.

1. Helgi Hrafn GunnarssonNovember 19, 2009 at 12:11 PM

Leave it to Microsoft to value a banker's way of doing things more than anyone in any other field. ;)
It's too bad that you can't set the default globally. Even worse, they'll never be able to replace this function without changing the name of it, without breaking backwards compatibility. How incredibly typical of Microsoft.

2. Sir I working with Microsoft Visual Studio 2005, so done Employee salary project in .Net with C# GUI Applications want coding Math Round function i.e. int x Value 236 but want return x value 240 end point is round.

3. "...to value a banker's way of doing things more than anyone in any other field." Why would you assert that? Statisticians also use this method. And in some countries it is the preferred instructional method. If may not be the way most people in their personal lives handle rounding, and it certainly wasn't included in our rounding lessons in grade school, those points I will grant. However, maybe we *should* use round to even in our personal lives, and maybe we *should* be teaching it in grade school. It seems to me that Microsoft, in this case, sided with experts in numerics rather than the average Joe. (Who knows more about rounding numbers than bankers and statisticians?)