Why does Convert.ToInt32() round to the nearest even number, instead of nearest whole number? [closed]
Asked Answered
C

4

21

Looking at the msdn documentation for Convert.ToInt32() it states:

If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.

http://msdn.microsoft.com/en-us/library/ffdk7eyz.aspx

Why is this?

Surely it would be more logical to round to the nearest whole number, wouldn't it? If so, 4.5 would become 5, and 5.5 would become 6, which seems to be more intuitive.

Crook answered 11/7, 2012 at 11:35 Comment(13)
If you want specific behavior use the Math.Round method. Of course Convert.ToInt32() does use this method already with the behavior described. It has to do with averages, you convert and add 6 numbers and half of them are rounded down and the other half are roudned up you get a more accurate number then if everything was rounded up or rounded down.Purpura
@Ramhound I appreciate that, I'm just curious as to why even has a preference over odd in any scenarioCrook
@Curt It is just an existing method. There is no reason why bankers rounding couldn't have favoured going to odd instead. The .NET implementation is simply piggy-backing off of something that already exists.Blane
en.wikipedia.org/wiki/Bankers_rounding#Round_half_to_evenAleut
When you say "Surely it would be more logical to round to the nearest whole number?", the whole problem is that there are two 'nearest whole numbers'...Aleut
It does return a nearest whole number, but there are two of those.Reversible
@Curt - The simply answer to your question is it would be less accurate. Even if they were to round to the nearest odd, that would sill involve rounding up half the time and rounding down half the time. Your question from a logical stand point does not make sense. When I spoke of the Convert.ToInt32 using "this method" I was speaking of behavior described in the documentation not Math.Round. Yes I downvoted this question, it seems you want to have a discussion about the reasons why the method works in this way, which seems sort of pointless to discuss.Purpura
@Aleut Cheers, I think I've spent so long seeing ".5" numbers being rounded up that I forgot its not actually closer to the higher whole number haha.Crook
@Curt I think this is often due to most schools (at least in England) only teaching rounding where .5 means "go up". I've fallen into the "what the heck is going on here?" trap with Bankers rounding.Blane
@AdamHouldsworth - It also is taught in the US. If the number is below .5 you round down, if its above .5 then you round up. If its .5 exactly then its entirely up to you. Always rounding up is can result in a number that is not entirely accuratePurpura
@Ramhound In that case, it is different from what I stated. We were taught that the decision of .5 was to "go up" by default for consistency.Blane
@Ramhound "up to you" seems a bit blaséCrook
I voted for reopening, as this seems a very valid question. It's about a reason for the behaviour observed, which is answerable.Chun
B
14

The History section of the Wikipedia entry for Rounding has some statements about the role of "round to even" in computing. Interestingly, it appears "Bankers Rounding" has little evidence to state it was official in any sense of the word, so can only be chalked up as slang terminology.

It is only "more logical" if you subscribe to that rounding mechanism. Bankers rounding (which is the default in this case) is also perfectly logical.

Imagine if banks rounded up to the nearest penny for every fractional amount, they would make a lot less (lose a lot of, for the cynical) money with the millions upon millions of transactions that are processed daily. OK, so this example is cynical.

Going towards the nearest even number (or odd, but history chose otherwise) means that not every rounding resolution goes up, some can now go down. When you put this to the law of averages, it becomes a fair solution to use when considering who is responsible for paying for the extra half penny.

As for why this was chosen for the framework, this question attempts to address it:

Why does .NET use banker's rounding as default?

Of course, this all harks back to financial days and its applicability to integral numbers could be questioned, but why bother? Accept it, override it if you want to, just understand how it works.


For people wondering how to change the default rounding:

If you are providing a non-integer to Convert.ToInt32 you will first actually need to do something like Convert.ToDouble and then Math.Round with the overload to change the rounding logic.

Blane answered 11/7, 2012 at 11:40 Comment(5)
I think you mean banks would make less money... I can't imaging they would ever lose anyUndercoating
@Undercoating But in the Banking world, is not making less money tantamount to losing it? You're in this country as well, you should have just as much cynicism towards the banks as I do lolBlane
@AdamHouldsworth Thanks Adam, I hadn't heard of Bankers rounding before and this has helped clear up my confusion.Crook
@Undercoating - But they don't actually make less money, we are talking half of a single penny in most cases, if you average all the transactions together then on average your at the very least even on the transaction. You should take some acounting courses before you make these comments.Purpura
@Ramhound: It was a joke regarding a comment made by Adam... maybe you should take some reading classes ;)Undercoating
K
6

This is exactly why the MidpontRounding overload was added to Math.Round.

Therefore for correct rounding you should use Math.Round rather than Convert.ToInt32.

Kim answered 11/7, 2012 at 11:38 Comment(4)
hmmm... yourself, along with the others, seem to be confused as to the queston. I don't see any part saying "how can I get consistency?", this is more about why the decision was made in the first place. I also agree it is not a great default behaviour if purposefully doneUndercoating
@Ramhound Possibly not.Blane
@AdamHouldsworth - Wouldn't this would make it a duplicate questionPurpura
@Ramhound On inspection yes, but there is some value in having a small number of duplicates as it feeds the duplicate detection machine. The duplicate in question would never have been returned in search results when typing the above question.Blane
B
3

Without considering the subjective question of whether MidpointRounding.ToEven (Banker's rounding) or MidpointRounding.AwayFromZero would be a better default.

When designing this, Microsoft would have considered the languages .NET was designed to replace.

  • VB classic has always used Banker's Rounding by default.

  • C / C++ conversions truncate when casting, and has library functions floor() and ceil() in the runtime library - but (AFAIK, may be wrong) no round function.

  • Java has a Math.round that in the documentation is described as being equivalent to Math.round(a+0.5). Which is arguably not what most people would expect for negative numbers (-3.5 rounds to -3).

  • VB developers may arguably be expected to need more handholding than developers coming from C/C++ or Java.

Hence it seems reasonable that when designing .NET, the class library would provide Floor, Ceiling and Round methods, and that the Round behavior would default to VB's behavior.

It also seems reasonable that Convert.ToInt32() would use the Round method (though I guess a case could be made for Floor, for consistency with casting).

Burkey answered 11/7, 2012 at 12:15 Comment(0)
T
2

If you want that behaviour you need to use Math.Round and specify MidpointRounding.AwayFromZero.

For example:

int result = (int)Math.Round(4.5, MidpointRounding.AwayFromZero);

Demo: http://ideone.com/ZAbBL

Convert.ToInt32(double) doesn't use Math.Round itself, instead it's implemented in this way (ILSpy):

public static int ToInt32(double value)
{
    if (value >= 0.0)
    {
        if (value < 2147483647.5)
        {
            int num = (int)value;
            double num2 = value - (double)num;
            if (num2 > 0.5 || (num2 == 0.5 && (num & 1) != 0))
            {
                num++;
            }
            return num;
        }
    }
    else
    {
        if (value >= -2147483648.5)
        {
            int num3 = (int)value;
            double num4 = value - (double)num3;
            if (num4 < -0.5 || (num4 == -0.5 && (num3 & 1) != 0))
            {
                num3--;
            }
            return num3;
        }
    }
    throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
Traynor answered 11/7, 2012 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.