Alex Rivera | Logout

What's wrong with casting 0.0 to double?

Asked 2010-11-06T12:26:53.057
28

I have InvalidCastException when I try to cast 0.0 to double, why is that so? It's fine when I do (float)value instead.

alt text

Edit
Report

1 Answer

10

That's normal. If the object type is float you cannot cast it to double because they are not of the same type:

object o = 1.0f;
double d = (double)o; // will throw an exception

You need to convert it:

double d = Convert.ToDouble(o);
answered 2010-11-06T12:31:32.917

Your Answer