C#교과서 마스터하기 3. 숫자 데이터 형식 사용

min seung moon·2021년 7월 9일
0

C#

목록 보기
3/54

https://www.youtube.com/watch?v=_BegVK2IhMs&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=12

1. 숫자 데이터 형식

  • sbyte, short, int, long
  • int, long. double, float, decimal, ...

2. 정수 데이터 형식

  • 정수형 타입 int의 최솟값과 최댓값
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int min = int.MinValue;
            int max = int.MaxValue;

            WriteLine(min);
            WriteLine(max);
        }
    }
}

3. 부호 있는 정수 데이터 형식

  • sbyte, short, int, long의 MaxValue 출력
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte sb = 127;
            short st = 32767;
            int i = int.MaxValue;
            long l = long.MaxValue;

            WriteLine("{0},{1},{2},{3}", sb, st, i, l);
        }
    }
}

4. 부호 없는 정수 데이터 형식

  • byte, ushort, uint, ulong
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b = byte.MaxValue;
            ushort s = ushort.MaxValue;
            uint i = uint.MaxValue;
            ulong l = ulong.MaxValue;

            WriteLine("{0},{1},{2},{3}", b, s, i, l);
        }
    }
}


5. 실수 데이터 형식

  • double : 실수형 데이터 형식(64비트 부동 소수점 숫자)
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            double d1 = 3.141592;
            double d2 = 3.141592D;
            double d3 = 3.141592d;

            WriteLine("{0}, {1}, {2}", d1, d2, d3);

        }
    }
}

  • float : 실수형 데이터 형식(32비트 부동 소수점 숫자)
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            float f1 = 3.141592F;
            float f2 = 3.141592f;

            WriteLine("{0}, {1}", f1, f2);

        }
    }
}

  • decimal : 실수형 데이터 형식(128비트 10진수), 금융권 프로젝트에서 사용
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal money1 = 12.34m;
            decimal money2 = 12.34M;

            WriteLine("{0}, {1}", money1, money2);

        }
    }
}

6. 숫자 형식의 리터럴 값에 접미사 붙이기

  • 원래 숫자형 타입은 null 타입으로 초기화 할 수 없고 0으로 초기화하여 사용했다
  • 하지만 최근에는 nullable 형식이 나왔기에 숫자형 타입에도 null로 초기화할 수 있게 되었다
    • type? name = null;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int? x = null;
            double? y = null;

            WriteLine("{0}, {1}",x, y);
        }
    }
}

profile
아직까지는 코린이!

0개의 댓글