int a = 500;
float b = (float)a;
표시 형식 | 메서드 |
---|---|
Decimal | Convert.ToDecimal(a) |
float | Convert.ToSingle(a) |
double | Convert.ToDouble(a) |
short | Convert.ToInt16(a) |
int | Convert.ToInt32(a) |
long | Convert.ToInt64(a) |
ushort | Convert.ToUInt16(a) |
uint | Convert.ToUInt32(a) |
ulong | Convert.ToUInt64(a) |
유니코드 | Convert.ToChar(a) |
문자열 | Convert.ToString(a) |
P . S
Console.WriteLine(Convert.ToInt32("12.345"))//문자열이 들어가면 FormatException발생
Console.WriteLine(Convert.ToInt32(null))//null이 들어가면 0을 출력
표시 형식 | 메서드 |
---|---|
Decimal | Decimal.Parse(a) |
float | Single.Parse(a) |
double | Double.Parse(a) |
short | Int16.Parse(a) |
int | Int32.Parse(a) |
long | Int64.Parse(a) |
ushort | UInt16.Parse(a) |
uint | UInt32.Parse(a) |
ulong | UInt64.Parse(a) |
char | Convert.ToChar(a) |
P . S
Int32.Parse(null)// ArgumetNullException 오류발생. null은 안됨
Int32.TryParse(a, out i)//a변수를 int형으로 변환이 가능한지 확인함. i에는 변환가능하면 10, 불가능하면 0을 반환.
Console.WriteLine(Int32.TryParse("10", out i) + ", i=" + i); //
Console.WriteLine(Int32.TryParse("12.345", out i) + ", i=" + i);
Console.WriteLine(Int32.TryParse(null, out i) + ", i=" + i);