Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 979
Curtis Avatar asked Jul 11 '12 11:07

Curtis


People also ask

Does convert ToInt32 round?

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.

Why do we use convert ToInt32?

ToInt32(String, IFormatProvider) Method. This method is used to converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information.

What is the meaning of convert ToInt32?

This method is used to convert the value of the specified Decimal to the equivalent 32-bit signed integer. A user can also convert a Decimal value to a 32-bit integer by using the Explicit assignment operator. Syntax: public static int ToInt32 (decimal value);

What is the meaning of convert ToInt32 in C#?

ToInt32(String, Int32) Converts the string representation of a number in a specified base to an equivalent 32-bit signed integer. ToInt32(UInt64) Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer.


1 Answers

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.

like image 61
Adam Houldsworth Avatar answered Sep 22 '22 14:09

Adam Houldsworth