435,361 Members | 3,185 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,361 IT Pros & Developers. It's quick & easy.

Convert.ToInt32(double) strange rounding behaviour

 P: n/a Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function Return Value value rounded to the nearest 32-bit signed integer. 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. Is there some obvious reason for this behaviour that I'm missing? Why would one want 4.5 to be rounded to 4 rather than 5? Confused, Norvin Nov 15 '05 #1
3 Replies

 P: n/a Norvin, This is called "Banker's rounding". Googling for that should give you more details. Mattias -- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ Please reply only to the newsgroup. Nov 15 '05 #2

 P: n/a "Norvin Laudon" a écrit dans le message de news:%2****************@TK2MSFTNGP09.phx.gbl... Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function Return Value value rounded to the nearest 32-bit signed integer. 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. Is there some obvious reason for this behaviour that I'm missing? Why would one want 4.5 to be rounded to 4 rather than 5? And, why would one want 4.5 to be rounded to 5 rather than 4? From a mathematical standpoint, 4.5 is halfway. So why would rounding it up make sense and rounding it down not make sense? Rounding always in the same direction increases the chances of getting strong deviations when you add lots of numbers that have been rounded. The banker's rounding rule tries to reduce this undesirable effect. Bruno Confused, Norvin Nov 15 '05 #3

 P: n/a Also, be ready for some surprises if you use float or double (see other posts on this ng). If you want predictable results for your rounding operations, you should use the decimal type. If you want to round half-up (or rather towards infinity, which means rounding half-down on negative numbers), you can use the following: static decimal RoundHalfTowardsInfinity(decimal dec) { return dec > 0 ? (int)(dec + 0.5m) : (int)(dec - 0.5m); } Side note for MS: it would be nice if the .NET framework provided rounding methods for the various rounding modes, aka. Java's BigDecimal. Bruno. "Bruno Jouhier [MVP]" a écrit dans le message de news:%2****************@TK2MSFTNGP12.phx.gbl... "Norvin Laudon" a écrit dans le message de news:%2****************@TK2MSFTNGP09.phx.gbl... Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function Return Value value rounded to the nearest 32-bit signed integer. 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. Is there some obvious reason for this behaviour that I'm missing? Why would one want 4.5 to be rounded to 4 rather than 5? And, why would one want 4.5 to be rounded to 5 rather than 4? From a mathematical standpoint, 4.5 is halfway. So why would rounding it up make sense and rounding it down not make sense? Rounding always in the same direction increases the chances of getting strong deviations when you add lots of numbers that have been rounded. The banker's rounding rule tries to reduce this undesirable effect. Bruno Confused, Norvin Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion. 