Home > Basics, Programming > Rounding in .NET: A fair warning

Rounding in .NET: A fair warning

Everybody uses the same rounding they learned in school. Only Siths deal in absolutes, as they say, but this is really really basic: .0, .1, .2, .3, .4 round down, and .5, .6, .7, .8, .9 round up. We’ve all learned this in school.

.NET Framework designers have taken it upon themselves to make fools of us all by defaulting the rounding algorithm (used by Math.Round) to something called “Banker’s rounding.” It behaves the same, except for .5, which will round to the nearest even integer. 2.5 rounds down to 2, 3.5 rounds up to 4. This was done so as to distribute .5 evenly up or down. Or a better explanation might be…


And hey, it’s not some totally made up thing if it’s got its own IEEE rule, right?

Fine, enough ranting. Here’s the way to round a number for the rest of us humans:

Math.Round(2.5, MidpointRounding.AwayFromZero); //Equals 3

The default is:

Math.Round(2.5, MidpointRounding.ToEven); //Equivalent to Math.Round(2.5); Equals 2
Categories: Basics, Programming Tags:
  1. 2014-09-18 at 10:22 am

    Hi. Sorry to bother you here, but could not ask on Stack as I just signed up. I’m using latest Wamp, Cordova and Laravel on win7 to develop an app that will need frequent access to database info on the server. Doing the simplest (quickstart – working) and a simple ajax request to display users. The jsonp user data is return to the dom and I can see it, but no matter what, it’s bypassing .done and hitting .fail and not executing the callback to replace the div with the data on the page. I can test with a google type search and works fine as is. Looks like same exact problem you reported and solved http://stackoverflow.com/questions/19770991/ajax-request-to-laravel-4-returns-200-ok-but-method-always-return-fail. I’ve added the after app filter mentioned as a potential answer to your post, but still have the same problem. I’m solid stuck and would very much appreciate if you could help me with this. Thanks in advance. Direct email d1@rjsig.com if that helps.

    • MPelletier
      2014-09-18 at 10:27 am

      I’m sorry, I didn’t ask that, I only cleanup the question. My knowledge of jquery is limited. Good luck.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: