<6t**@qlink.que ensu.ca> wrote:
No offense to the posters (Bob and KBuser), but you haven't really
answered my question.
Well, they answered why you got the answer 419. You're multiplying
4.1999998092651 3671875 by 100. There's no rounding *error* - there's
just a different method of rounding for casts than for Convert.ToInt32 .
<snip>
Perhaps, I wasn't being clear enough in my original post, if I use
Convert.ToInt32 then I get 420, not 419. So obviously the Convert
method is doing something to make the result more accurate.
Well, it's choosing a different method of rounding.
From the docs for Convert.ToInt32 :
<quote>
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>
From the C# 1.1 language spec, (ECMA numbering) section 13.2.1:
<quote>
For a conversion from float or double to an integral type, the
processing depends on the overflow-checking context (§14.5.12) in which
the conversion takes place:
....
In an unchecked context, the conversion always succeeds, and proceeds
as follows:
If the value of the source operand is within the range of the
destination type, then it is rounded towards zero to the nearest
integral value of the destination type, and this integral value is the
result of the conversion.
</quote>
So, one is rounding to nearest, the other is rounding towards zero.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too