This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
Explicit Numeric Conversions Table
Explicit numeric conversion is used to convert any numeric type to any other numeric type, for which there is no implicit conversion, by using a cast expression. The following table shows these conversions.
From |
To |
sbyte |
byte, ushort, uint, ulong, or char |
byte |
sbyte or char |
short |
sbyte, byte, ushort, uint, ulong, or char |
ushort |
sbyte, byte, short, or char |
int |
sbyte, byte, short, ushort, uint, ulong, or char |
uint |
sbyte, byte, short, ushort, int, or char |
long |
sbyte, byte, short, ushort, int, uint, ulong, or char |
ulong |
sbyte, byte, short, ushort, int, uint, long, or char |
char |
sbyte, byte, or short |
float |
sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal |
double |
sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal |
decimal |
sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double |
Remarks
- The explicit numeric conversion may cause loss of precision or result in throwing exceptions.
- When you convert a float, double, or decimal value to an integral type, this value is rounded towards zero to the nearest integral value. If the resulting integral value is outside the range of the destination type, an InvalidCastException is thrown.
- When you convert double to float, the double value is rounded to the nearest float value. If the double value is too small or too large to fit into the destination type, the result will be zero or infinity.
- When you convert float or double to decimal, the source value is converted to decimal representation and rounded to the nearest number after the 28th decimal place if required. Depending on the value of the source value, one of the following results may occur:
- If the source value is too small to be represented as a decimal, the result becomes zero.
- If the source value is NaN (not a number), infinity, or too large to be represented as a decimal, an InvalidCastException is thrown.
- When you convert decimal to float or double, the decimal value is rounded to the nearest double or float value.
For more information on explicit conversion, see the language reference, §6.2.
See Also
Integral Types Table | Built-in Types Table | Implicit Numeric Conversions Table