Explicit Numeric Conversions Table (C# Reference)

 

Updated: July 20, 2015

System_CAPS_ICON_note.jpg Note

For the latest documentation on C#, visit the C# Guide on docs.microsoft.com.

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.

For more information about conversions, see Casting and Type Conversions.

FromTo
sbytebyte, ushort, uint, ulong, or char
byteSbyte or char
shortsbyte, byte, ushort, uint, ulong, or char
ushortsbyte, byte, short, or char
intsbyte, byte, short, ushort, uint, ulong,or char
uintsbyte, byte, short, ushort, int, or char
longsbyte, byte, short, ushort, int, uint, ulong, or char
ulongsbyte, byte, short, ushort, int, uint, long, or char
charsbyte, byte, or short
floatsbyte, byte, short, ushort, int, uint, long, ulong, char,or decimal
doublesbyte, byte, short, ushort, int, uint, long, ulong, char, float,or decimal
decimalsbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double
  • The explicit numeric conversion may cause loss of precision or result in throwing exceptions.

  • When you convert a 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 OverflowException is thrown.

  • When you convert from a double or float value to an integral type, the value is truncated. If the resulting integral value is outside the range of the destination value, the result depends on the overflow checking context. In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.

  • 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 OverflowException 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 Explicit in the C# Language Specification. For more information on how to access the spec, see C# Language Specification.

C# Reference
C# Programming Guide
Casting and Type Conversions
() Operator
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table

Show: