Explicit Numeric Conversions
suggest changeExplicit 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents