Contents
접기
728x90
1. 변수 만들기
level
,count
의 이름을 가진 정수형 변수를 만들어주세요.percentage
,speed
의 이름을 가진 실수형 변수를 만들어주세요.nickname
,description
의 이름을 가진 문자형 변수를 만들어주세요.
//정수형
int level;
int count;
//실수형
float percentage;
float speed;
//문자형
string nickname;
string description;
2. 변수에 데이터 입력
int level = 1;
int count = 2;
float percentage = 1.1f;
float speed = 2.3f;
string nickname = "도치";
string description="고슴도치이다";
3. (숫자 <-> 숫자) 형변환
int iTen = 10;
float fTen = (float)iTen; //10정수형을 10실수형으로 바꿀 경우에 데이터 손실이 잃어나지 않기 때문에 float를 붙이지 않아도 된다.
float fFive = 5.5f;
int iFive = (int)fFive;
4. (숫자 -> 문자) 형변환
int n = 10;
string toStringN = n.ToString();
float f = 0.5f;
string toStringF = f.ToString();
5. (문자 -> 숫자) 형변환
string strTen = "10";
string strSix = "6.2";
//Convert이용
int intTen = Convert.ToInt32(strTen);
float floatSix = Convert.ToSingle(strSix);
//Parse이용
int intTen = int.Parse(strTen);
float floatSix = float.Parse(strSix);
(THINK) Convert 와 Parse 는 어떤 차이가 있을까?
Convert 클래스는 여러 자료형에서 다른 여러 자료형들로의 형변환 가능
Parse() 함수는 오직 문자열에서 다른 자료형들로의 형변환만 가능
728x90
'C# > 데이터다루기' 카테고리의 다른 글
[C#] Escape Sequence (0) | 2025.04.14 |
---|---|
[C#] Console.ReadLine(), Console.WriteLine(), Console.Read(), Console.Write() (0) | 2025.03.20 |
[C#] 형변환-TryParse (0) | 2025.03.20 |
[C#] 자료형 (0) | 2025.03.18 |
[C#] 변수 (0) | 2025.03.18 |