The ChangeType() method returns an object of the specified type and whose value is equivalent to the specified object.
Let’s say we have a double type.
double val = -3.456
Now, use the ChangeType method to change the type to integer.
num = (int)Convert.ChangeType(val, TypeCode.Int32);
Let us see the complete example.
Example
using System;
public class Demo {
public static void Main() {
double val = -3.456;
int num = (int)Convert.ChangeType(val, TypeCode.Int32);
Console.WriteLine("{0} converted to an Int32: {1}", val, num);
}
}Output
-3.456 converted to an Int32: -3