Math.Round broken?

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]
 
C

Codemonkey

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem With Round Function 9
Math.Round error? 7

Top