Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Banker's Rounding?

Banker's Rounding?

Scheduled Pinned Locked Moved The Lounge
csharphtmlcomquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Joshua Quick

    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.

    N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #2

    Cast to int and then back to float/double? [edit] Er, add 0.5, then cast to int and then back to float/double? [/edit]

    J 2 Replies Last reply
    0
    • N Nish Nishant

      Cast to int and then back to float/double? [edit] Er, add 0.5, then cast to int and then back to float/double? [/edit]

      J Offline
      J Offline
      Joshua Quick
      wrote on last edited by
      #3

      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.

      1 Reply Last reply
      0
      • J Joshua Quick

        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.

        J Offline
        J Offline
        Joshua Quick
        wrote on last edited by
        #4

        D'oh!

        1 Reply Last reply
        0
        • J Joshua Quick

          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.

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #5

          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"

          J P 2 Replies Last reply
          0
          • R Ryan Binns

            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"

            J Offline
            J Offline
            Joshua Quick
            wrote on last edited by
            #6

            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.

            C A 2 Replies Last reply
            0
            • N Nish Nishant

              Cast to int and then back to float/double? [edit] Er, add 0.5, then cast to int and then back to float/double? [/edit]

              J Offline
              J Offline
              Joshua Quick
              wrote on last edited by
              #7

              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. ;)

              N 1 Reply Last reply
              0
              • J Joshua Quick

                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. ;)

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #8

                Yep :-)

                1 Reply Last reply
                0
                • J Joshua Quick

                  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.

                  C Offline
                  C Offline
                  cmk
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • R Ryan Binns

                    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"

                    P Offline
                    P Offline
                    peterchen
                    wrote on last edited by
                    #10

                    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

                    1 Reply Last reply
                    0
                    • J Joshua Quick

                      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.

                      A Offline
                      A Offline
                      Andrew Peace
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups