What is the difference between Convert.ToInt32 and (int) Parsing

I can't understand the difference between Convert.ToInt32 and Parsing (int) command when i convert a double number to a int number.My example code is here and i have two different answer when i show it.

class Program
{
    static void Main(string[] args)
    {
        double i = 3.897456465;
        int y;
        y = Convert.ToInt32(i);
        Console.WriteLine(y);
        y = (int)i;
        Console.WriteLine(y);
        Console.ReadKey();
    }
}
Jon Skeet
people
quotationmark

From the documentation from Convert.ToInt32(double):

Return Value
Type: System.Int32
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.

From the C# 5 specification section 6.2.1, explicit numeric conversions:

For a conversion from float or double to an integral type [...]

  • [...]
  • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.

(Emphasis mine.)

So basically, Convert.ToInt32 rounds up or down to the nearest int. Casting always rounds towards zero.

people

See more on this question at Stackoverflow