기본 제공 숫자 변환(C# 참조)Built-in numeric conversions (C# reference)
C#에서는 정수 및 부동 소수점 숫자 형식 집합을 제공합니다.C# provides a set of integral and floating-point numeric types. 두 숫자 형식 간에는 암시적 또는 명시적 변환이 있습니다.There exists a conversion between any two numeric types, either implicit or explicit. 명시적 변환을 수행하려면 캐스트 식 사용해야 합니다.You must use a cast expression to perform an explicit conversion.
암시적 숫자 변환Implicit numeric conversions
다음 표에서는 기본 제공 숫자 형식 간의 미리 정의된 암시적 숫자 변환을 보여 줍니다.The following table shows the predefined implicit conversions between the built-in numeric types:
시작From | 대상To |
---|---|
sbytesbyte | short , int , long , float , double 또는 decimal short , int , long , float , double , or decimal |
bytebyte | short , ushort , int , uint , long , ulong , float , double 또는 decimal short , ushort , int , uint , long , ulong , float , double , or decimal |
shortshort | int , long , float , double 또는 decimal int , long , float , double , or decimal |
ushortushort | int , uint , long , ulong , float , double 또는 decimal int , uint , long , ulong , float , double , or decimal |
intint | long , float , double 또는 decimal long , float , double , or decimal |
uintuint | long , ulong , float , double 또는 decimal long , ulong , float , double , or decimal |
longlong | float , double 또는 decimal float , double , or decimal |
ulongulong | float , double 또는 decimal float , double , or decimal |
floatfloat | double |
참고
int
, uint
, long
또는 ulong
에서 float
로 암시적 변환하거나 long
또는 ulong
에서 double
로 암시적 변환하는 경우 정밀도가 손실될 수도 있지만, 자릿수 손실은 없습니다.The implicit conversions from int
, uint
, long
, or ulong
to float
and from long
or ulong
to double
may cause a loss of precision, but never a loss of an order of magnitude. 다른 암시적 숫자 변환 시에는 정보 손실이 없습니다.The other implicit numeric conversions never lose any information.
다음 사항에도 유의하세요.Also note that
정수 숫자 형식을 부동 소수점 숫자 형식으로 암시적으로 변환할 수 있습니다.Any integral numeric type is implicitly convertible to any floating-point numeric type.
byte
및sbyte
형식으로의 암시적 변환은 없습니다.There are no implicit conversions to thebyte
andsbyte
types.double
및decimal
형식에서 암시적 변환은 없습니다.There are no implicit conversions from thedouble
anddecimal
types.decimal
형식과float
또는double
형식 간의 암시적 변환은 없습니다.There are no implicit conversions between thedecimal
type and thefloat
ordouble
types.대상 형식의 범위 내에 있는 경우
int
형식의 상수 식 값(예: 정수 리터럴로 표시된 값)을sbyte
,byte
,short
,ushort
,uint
또는ulong
으로 암시적으로 변환할 수 있습니다.A value of a constant expression of typeint
(for example, a value represented by an integer literal) can be implicitly converted tosbyte
,byte
,short
,ushort
,uint
, orulong
, if it's within the range of the destination type:byte a = 13; byte b = 300; // CS0031: Constant value '300' cannot be converted to a 'byte'
위 예제에서 볼 수 있듯이, 상수 값이 대상 형식의 범위 내에 있지 않으면 컴파일러 오류 CS0031이 발생합니다.As the preceding example shows, if the constant value is not within the range of the destination type, a compiler error CS0031 occurs.
명시적 숫자 변환Explicit numeric conversions
다음 표에서는 암시적 변환이 없는 기본 제공 숫자 형식 간의 미리 정의된 명시적 변환을 보여 줍니다.The following table shows the predefined explicit conversions between the built-in numeric types for which there is no implicit conversion:
시작From | 대상To |
---|---|
sbytesbyte | byte , ushort , uint 또는 ulong byte , ushort , uint , or ulong |
bytebyte | sbyte |
shortshort | sbyte , byte , ushort , uint 또는 ulong sbyte , byte , ushort , uint , or ulong |
ushortushort | sbyte , byte 또는 short sbyte , byte , or short |
intint | sbyte , byte , short , ushort , uint 또는 ulong sbyte , byte , short , ushort , uint , or ulong |
uintuint | sbyte , byte , short , ushort 또는 int sbyte , byte , short , ushort , or int |
longlong | sbyte , byte , short , ushort , int , uint 또는 ulong sbyte , byte , short , ushort , int , uint , or ulong |
ulongulong | sbyte , byte , short , ushort , int , uint 또는 long sbyte , byte , short , ushort , int , uint , or long |
floatfloat | sbyte , byte , short , ushort , int , uint , long , ulong 또는 decimal sbyte , byte , short , ushort , int , uint , long , ulong , or decimal |
doubledouble | sbyte , byte , short , ushort , int , uint , long , ulong , float 또는 decimal sbyte , byte , short , ushort , int , uint , long , ulong , float , or decimal |
decimaldecimal | sbyte , byte , short , ushort , int , uint , long , ulong , float 또는 double sbyte , byte , short , ushort , int , uint , long , ulong , float , or double |
참고
명시적 숫자 변환으로 인해 데이터가 손실되거나 예외(일반적으로 OverflowException)가 throw될 수 있습니다.An explicit numeric conversion might result in data loss or throw an exception, typically an OverflowException.
다음 사항에도 유의하세요.Also note that
정수 형식의 값을 다른 정수 형식으로 변환하면 결과는 오버플로 검사 컨텍스트에 따라 달라집니다.When you convert a value of an integral type to another integral type, the result depends on the overflow checking context. 확인된 컨텍스트에서 소스 값이 대상 형식의 범위 내에 있으면 변환이 성공합니다.In a checked context, the conversion succeeds if the source value is within the range of the destination type. 그렇지 않으면 OverflowException이 throw됩니다.Otherwise, an OverflowException is thrown. 확인되지 않은 컨텍스트에서 변환은 항상 성공하고 다음과 같이 진행됩니다.In an unchecked context, the conversion always succeeds, and proceeds as follows:
소스 형식이 대상 형식보다 큰 경우 소스 값은 가장 중요한 비트인 해당 "extra"를 삭제함으로써 잘립니다.If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits. 그런 다음, 결과는 대상 형식의 값으로 처리됩니다.The result is then treated as a value of the destination type.
소스 형식이 대상 형식보다 작은 경우 소스 값이 대상 형식과 크기가 같도록 부호 확장 또는 0 확장됩니다.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. 부호 확장은 소스 형식이 서명된 경우 사용되며, 소스 형식이 서명되지 않은 경우 0 확장이 사용됩니다.Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned. 그런 다음, 결과는 대상 형식의 값으로 처리됩니다.The result is then treated as a value of the destination type.
소스 형식이 대상 형식과 동일한 크기인 경우 소스 값은 대상 형식의 값으로 처리됩니다.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.
decimal
값을 정수 형식으로 변환하면, 이 값은 0을 향한 방향으로 가장 가까운 정수값으로 반올림됩니다.When you convert adecimal
value to an integral type, this value is rounded towards zero to the nearest integral value. 결과 정수 값이 대상 형식 범위에서 벗어났을 경우 OverflowException이 throw됩니다.If the resulting integral value is outside the range of the destination type, an OverflowException is thrown.double
또는float
값을 정수 형식으로 변환하면, 이 값은 0을 향한 방향으로 가장 가까운 정수값으로 반올림됩니다.When you convert adouble
orfloat
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, the result depends on the overflow checking context. Checked 컨텍스트에서는 OverflowException이 throw됩니다. 반면 Unchecked 컨텍스트에서 결과는 지정되지 않은 대상 형식 값이 됩니다.In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.double
을float
로 변환할 경우double
값을 가장 가까운float
값으로 반올림합니다.When you convertdouble
tofloat
, thedouble
value is rounded to the nearestfloat
value.double
값이 너무 크거나 작아서float
형식에 맞지 않는 경우 결과 값은 0 또는 무한대가 됩니다.If thedouble
value is too small or too large to fit into thefloat
type, the result is zero or infinity.float
또는double
을decimal
로 변환할 경우 소스 값을decimal
표현으로 변환한 다음 필요한 경우 가장 가까운 소수점 이하 28번째 자리로 반올림합니다.When you convertfloat
ordouble
todecimal
, the source value is converted todecimal
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:소스 값이 너무 작아서
decimal
로 나타낼 수 없을 경우 결과 값은 0이 됩니다.If the source value is too small to be represented as adecimal
, the result becomes zero.소스 값이 NaN(숫자가 아님), 무한대 또는 너무 커서
decimal
로 나타낼 수 없을 경우 OverflowException을 throw합니다.If the source value is NaN (not a number), infinity, or too large to be represented as adecimal
, an OverflowException is thrown.
decimal
을float
또는double
로 변환하는 경우 소스 값이 각각 가장 가까운float
또는double
값으로 반올림됩니다.When you convertdecimal
tofloat
ordouble
, the source value is rounded to the nearestfloat
ordouble
value, respectively.
C# 언어 사양C# language specification
자세한 내용은 C# 언어 사양의 다음 섹션을 참조하세요.For more information, see the following sections of the C# language specification: