472,804 Members | 810 Online

# Convert.ToInt32(double) strange rounding behaviour

Hi,

Can somebody explain the following, from the MSDN documentation for the
"System.Convert.ToInt32(double)" function

<quote>
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.
</quote>

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 6637
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/
Nov 15 '05 #2
"Norvin Laudon" <no************@molinospamenergy.com> 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

<quote>
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.
</quote>

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
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]" <bj******@club-internet.fr> a écrit dans le message de
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Norvin Laudon" <no************@molinospamenergy.com> 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

<quote>
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.
</quote>

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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 by: jeff_zhang446 | last post by: Hi, I try to convert double to string as below: std::string cnvrtToString(double lValue) { std::ostringstream lStream; lStream << lValue; return lStream.str(); 12 by: Alan | last post by: how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I... 2 by: Pascal | last post by: Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type... 4 by: Stumped | last post by: double DesignGlassPriceDbl =WindowAreaSqFtInt * DesignGlassPricePSF; int DesignGlassPriceInt= System.ConvertToInt32(DesignGlassPriceDbl); In the above code, if "DesignGlassPriceDbl" is 213.33,... 3 by: mlafarlett | last post by: In the example below, the 'convert.ToDecimal' removes the trailing 5. I'd like to get the number, in tact, moved to the decimal variable. Any help would be greatly appreciated. Dim dcml As... 0 by: aparna12 | last post by: Hi all Please tell me how to Convert double to string in fraction format (.75 to 3/4) in asp.net. Thanks Aparna 1 by: =?big5?B?qM6nuw==?= | last post by: I already knew how to convert bytes to Double, use BitConverter.ToDouble(). but, How to convert double to bytes? for example: double a = 3,444; I want convert to bytes={ ?, ?, ?, ?, ?, ?,... 21 by: Aman JIANG | last post by: hi I need to do this (convert double to string) fast, safe and portable. Is there any way to do this ? Except the ways following: 1. C++ I/O stream, stringstream (and boost::lexical_cast) 2.... 1 by: Bjorn Brox | last post by: Hi! In germany, norway and France(?) we are using ',' as decimal separator and it always messes up when you convert a double to and from a string where the interface expects double values stored... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central... 0 by: linyimin | last post by: Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for... 0 by: Taofi | last post by: I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ... 14 by: DJRhino1175 | last post by: When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If... 0 by: Rina0 | last post by: I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in... 0 by: lllomh | last post by: Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{ 0 by: lllomh | last post by: How does React native implement an English player? 0 by: Mushico | last post by: How to calculate date of retirement from date of birth 2 by: DJRhino | last post by: Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...