C#프로그래밍

변환(캐스팅)

다모아 2023. 7. 19. 17:08
using System;
using System.ComponentModel;

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            float floatHeroHp = 10.56f;
            int intHeroHp = (int)floatHeroHp;
            //캐스트 연산자 , 캐스트 식, 명시적 변환, 캐스팅
            Console.WriteLine(intHeroHp);

            //int num = "홍길동";
            //문자열 (숫자형) -> 숫자 (정수)
            int num = Convert.ToInt32(Convert.ToInt32("123"));
            Console.WriteLine(num);

            //숫자 -> 문자열
           string str = Convert.ToString(num);
            Console.WriteLine(str);

            //정수 -> 실수
            float a = Convert.ToSingle(123);
            Console.WriteLine("exp: {0:0.00}%", a);

            //실수 -> 정수
            int damage = Convert.ToInt32(123.33);
            Console.WriteLine("damage: {0}", damage);

            float aa = (float)23; // 암시적 변환
            Console.WriteLine(aa);
        }
    }
}

'C#프로그래밍' 카테고리의 다른 글

스타크래프트  (0) 2023.07.19
영웅 피  (0) 2023.07.19
디아블로 아이템 사전  (0) 2023.07.19
문자열 보간  (0) 2023.07.19
결합연산자  (0) 2023.07.19