Math.Round broken?

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

the documentation states that Math.Round supports banker's rounding but it
also states that if the last number is 5 it will rounded up if the whole
number is even, otherwise rounded down which is totally bullshit!

i expect:
Round(1.344, 2) == 123.34
Round(1.345, 2) == 123.35

and nothing else!
for a workaround i had to write my own rounding method (not tested):

public static decimal Round(decimal val, int decimals)
{
decimal factor = (val * (decimal)Math.Pow(10, 2));
decimal tmp = Math.Floor(factor);
if (val - tmp >=0.5m)
tmp++;
return tmp/factor;
}

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no ****ing ads]
 
Bankers rounding is also known as IEEE Standard 754, section 4.

Because "5" is a special case when rounding (we don't know whether it should
be rounded up or down), bankers rounding gives a better distribution of
rounding for a larger set of values.

"5" is a special case because the digits 1,2,3,4 should be rounded down and
the digits 6,7,8,9 should be rounded up. If we always rounded 5 up, this
would mean that on average, more numbers would be rounded up. Bankers
rounding gets around this by basing the rounding of "5" on whether the
number is odd or even. Thus on average 5 will be rounded up half the time
and rounded down the other half.

Hope my explanation has clarified it enough so it won't be called total
bullshit. I'd hate it if most software (and CPU's!) were bullshit ;-)


Trev.
 
Back
Top