Banker's Rounding?
-
Bummer! :doh: I've just found out that .NET's Math.Round() function doesn't round the way I expected it to. It implement banker's rounding, which means it'll round up or down on the .5 part depending if the whole number part is even or odd. Math.Round on MSDN[^] Math.Round(3.5) -> 4.0 Math.Round(4.5) -> 4.0 What the? :wtf: M'eh! Guess I can't go home yet. Gotta' replace all those Round functions of mine. I just needed to vent a little first.
Cast to
int
and then back tofloat
/double
? [edit] Er, add 0.5, then cast toint
and then back tofloat
/double
? [/edit] -
Cast to
int
and then back tofloat
/double
? [edit] Er, add 0.5, then cast toint
and then back tofloat
/double
? [/edit]Hi Nishant! Yeah, I know that trick. I wasn't asking a programming question. I was just venting. I'm just a little surprised Microsoft chose to implement it that way.
-
Bummer! :doh: I've just found out that .NET's Math.Round() function doesn't round the way I expected it to. It implement banker's rounding, which means it'll round up or down on the .5 part depending if the whole number part is even or odd. Math.Round on MSDN[^] Math.Round(3.5) -> 4.0 Math.Round(4.5) -> 4.0 What the? :wtf: M'eh! Guess I can't go home yet. Gotta' replace all those Round functions of mine. I just needed to vent a little first.
D'oh!
-
Bummer! :doh: I've just found out that .NET's Math.Round() function doesn't round the way I expected it to. It implement banker's rounding, which means it'll round up or down on the .5 part depending if the whole number part is even or odd. Math.Round on MSDN[^] Math.Round(3.5) -> 4.0 Math.Round(4.5) -> 4.0 What the? :wtf: M'eh! Guess I can't go home yet. Gotta' replace all those Round functions of mine. I just needed to vent a little first.
It's interesting. That's exactly how I was taught rounding in school. Apparently (so I've heard), people in Scotland and Perth (Western Australia, where I am) are taught differently to the rest of world, in that we are taught to round to the even number, while everyone else is taught to round up (or away from 0, I can't remember which).
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
It's interesting. That's exactly how I was taught rounding in school. Apparently (so I've heard), people in Scotland and Perth (Western Australia, where I am) are taught differently to the rest of world, in that we are taught to round to the even number, while everyone else is taught to round up (or away from 0, I can't remember which).
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Hmmm... you're right, this is interesting. Makes me wonder what the history behind banker's rounding is and what other countries use it. In my case, it's not what I expected and it certainly does not match Excel's rounding behavior.
-
Cast to
int
and then back tofloat
/double
? [edit] Er, add 0.5, then cast toint
and then back tofloat
/double
? [/edit]Oops! I see one error in there Nishant. I almost missed it too. You have to offset negative numbers by -0.5 to make them round correctly. ;)
-
Oops! I see one error in there Nishant. I almost missed it too. You have to offset negative numbers by -0.5 to make them round correctly. ;)
Yep :-)
-
Hmmm... you're right, this is interesting. Makes me wonder what the history behind banker's rounding is and what other countries use it. In my case, it's not what I expected and it certainly does not match Excel's rounding behavior.
I believe it is considered statistically more 'fair'. Over a large population it is more likely that the sum of the roundings will cancel out. The more traditional method of rounding to the nearest integer (3.6 -> 4, 3.4 -> 3) can be very unfair in that some calculations may have a tendency to result in a similar fraction portion, thus causing the same rounding to take place. By rounding on the integer part as opposed the fraction you take this bias out. This is important in dealing with rounding currency - hence the term bankers rounding. ...cmk Save the whales - collect the whole set
-
It's interesting. That's exactly how I was taught rounding in school. Apparently (so I've heard), people in Scotland and Perth (Western Australia, where I am) are taught differently to the rest of world, in that we are taught to round to the even number, while everyone else is taught to round up (or away from 0, I can't remember which).
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Me too, but now (after the wall came down) kids learn "away from 0".
Pandoras Gift #44: Hope. The one that keeps you on suffering.
aber.. "Wie gesagt, der Scheiss is' Therapie"
boost your code || Fold With Us! || sighist | doxygen -
Hmmm... you're right, this is interesting. Makes me wonder what the history behind banker's rounding is and what other countries use it. In my case, it's not what I expected and it certainly does not match Excel's rounding behavior.
Joshua Quick wrote: Makes me wonder what the history behind banker's rounding is and what other countries use it. Banker's rounding gives the data better statistical qualities because it doesn't introduce a bias into the data. [edit] I really should read all the other posts before making my own - sorry cmk! [/edit]. -- Andrew.