Explicit Numeric Conversions

suggest change

Explicit casting operators can be used to perform conversions of numeric types, even though they don’t extend or implement one another.

double value = -1.1;
int number = (int) value;

Note that in cases where the destination type has less precision than the original type, precision will be lost. For example, -1.1 as a double value in the above example becomes -1 as an integer value.

Also, numeric conversions rely on compile-time types, so they won’t work if the numeric types have been “boxed” into objects.

object value = -1.1;
int number = (int) value; // throws InvalidCastException

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents