Depending on the value of the source value, one of the following results may occur: ![]() 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 necessary. If the double value is too small or too large to fit into the float type, the result is zero or infinity. When you convert double to float, the double value is rounded to the nearest float value. In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type. If the resulting integral value is outside the range of the destination type, the result depends on the overflow checking context. When you convert a double or float 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 a decimal value to an integral type, this value is rounded towards zero to the nearest integral value. ![]() If the source type is the same size as the destination type, then the source value is treated as a value of the destination type. The result is then treated as a value of the destination type. Sign-extension is used if the source type is signed zero-extension is used if the source type is unsigned. If the source type is smaller than the destination type, then the source value is either sign-extended or zero-extended so that it's of the same size as the destination type. If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits. In an unchecked context, the conversion always succeeds, and proceeds as follows: Otherwise, an OverflowException is thrown. In a checked context, the conversion succeeds if the source value is within the range of the destination type. When you convert a value of an integral type to another integral type, the result depends on the overflow checking context. Sbyte, byte, short, ushort, int, uint, long, or nintĪn explicit numeric conversion might result in data loss or throw an exception, typically an OverflowException. Sbyte, byte, short, ushort, int, uint, ulong, or nuint Sbyte, byte, short, ushort, int, uint, long, ulong, float, double, nint, or nuint Sbyte, byte, short, ushort, int, uint, long, ulong, float, decimal, nint, or nuint Sbyte, byte, short, ushort, int, uint, long, ulong, decimal, nint, or nuint Sbyte, byte, short, ushort, int, uint, long, nint, or nuint ![]() Sbyte, byte, short, ushort, int, uint, ulong, nint, or nuint Sbyte, byte, short, ushort, uint, ulong, or nuint Sbyte, byte, ushort, uint, ulong, or nuint The following table shows the predefined explicit conversions between the built-in numeric types for which there is no implicit conversion: From There are no implicit conversions between the decimal type and the float or double types.Ī value of a constant expression of type int (for example, a value represented by an integer literal) can be implicitly converted to sbyte, byte, short, ushort, uint, ulong, nint, or nuint, if it's within the range of the destination type: byte a = 13 īyte b = 300 // CS0031: Constant value '300' cannot be converted to a 'byte'Īs the preceding example shows, if the constant value is not within the range of the destination type, a compiler error CS0031 occurs. There are no implicit conversions from the double and decimal types. There are no implicit conversions to the byte and sbyte types. The other implicit numeric conversions never lose any information.Īny integral numeric type is implicitly convertible to any floating-point numeric type. The implicit conversions from int, uint, long, ulong, nint, or nuint to float and from long, ulong, nint, or nuint to double may cause a loss of precision, but never a loss of an order of magnitude.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |