본문 바로가기
C#/Basic Syntax

[C#] 2. 숫자 데이터 형식 사용하기

by 가든가든 2022. 9. 7.
728x90

프로그래밍을 하다 보면 정수 및 실수 데이터를 자주 사용합니다.

C#에서는 숫자 형식의 데이터를 다룰 때 사용하는 int 키워드를 포함하여 여러 가지 키워드를 제공합니다.

숫자 데이터 형식은 크게 정수 데이터 형식과 소수점이 있는 실수 데이터 형식으로 나누며, 다시 부호 있는 숫자와 부호 없는 숫자로 나눕니다.

숫자 데이터 형식

  • 숫자 데이터 형식 → 정수 데이터 형식, 실수 데이터 형식
  • 정수 데이터 형식 → 부호 있는 숫자(+, -), 부호 없는 숫자(+)

정수 데이터 형식

정수형 키워드에는 s와 u 접두사가 붙는데 이는 signed와 unsigned의 약자로, 부호를 붙이느냐 붙이지 않느냐 하는 차이가 있습니다.

  • 부호 있는(signed): +, - 부호가 있는 정수형입니다. 즉, 양수와 음수를 모두 지원합니다.
  • 부호 없는(unsigned): 부호 없이 + 값만 다루는 정수형입니다. 즉, 양수만 지원합니다.

부호 있는 정수 데이터 형식

데이터 형식 비트 범위 닷넷 형식

sbyte 8비트 -128 ~ +127 System.SByte
short 16비트 -32768 ~ +32767 System.Int16
int 32비트 -2147483648 ~ +2147483647 System.Int32
long 64비트 –9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 System.Int64

부호 없는 정수 데이터 형식

데이터 형식 비트 범위 닷넷 형식

byte 8비트 0 ~ 255 System.SByte
ushort 16비트 0 ~ 65,535 System.UInt16
uint 32비트 0 ~ 4,294,967,295 System.UInt32
ulong 64비트 0 ~ 18,446,744,073,709,551,615 System.UInt64

코드 예제

//Integer.cs
using System;

namespace N_NumberValue
{
    class NumberValue
    {
        public void NumberValueEx()
        {
            int min = -2147483648; //정수 최솟값
            int max = +2147483647; //정수 최댓값
						//초과시 에러 발생
            Console.WriteLine("int 변수의 최솟값 : {0}", min);
            Console.WriteLine("int 변수의 최댓값 : {0}", max);

            Console.WriteLine();

            byte iByte = 255;
            ushort iUInt16 = 65535;
            uint iUInt32 = 4294967295;
            ulong iUInt64 = 18446744073709551615;

            Console.WriteLine("8비트 byte : {0}", iByte);
            Console.WriteLine("16비트 ushort : {0}", iUInt16);
            Console.WriteLine("32비트 uint : {0}", iUInt32);
            Console.WriteLine("64비트 ulong : {0}", iUInt64);

            Console.WriteLine();
            //MinValue와 MaxValue 속성으로 최솟값과 최댓값 출력
            Console.WriteLine("[32비트] int 최솟값 : {0}", int.MinValue);
            Console.WriteLine("[32비트] int 최댓값 : {0}", int.MaxValue);
        }
    }
}
//Day2_Main.cs
using System;
using N_WriteLine;
using N_Placeholder;
using N_Value;
using N_NumberValue;

namespace Main
{
    class Day2_Main
    {
        static void Main(string[] args)
        {
            //WriteLine A = new WriteLine();
            //A.WriteEx();

            //Placeholder B = new Placeholder();
            //B.PlaceholderEx();

            //Value C = new Value();
            //C.ValueEx();

            NumberValue D = new NumberValue();
            D.NumberValueEx();
        }
    }
}

정수 숫자 형식 - C# 참조

실수 데이터 형식

실수 데이터는 부동소수점 방식과 10진 방식을 다루는 세 가지 데이터 형식 키워드인 double, float, decimal을 제공합니다.

double, float - 부동 소수점 방식

decimal - 10진 방식이며, 소수점 28자리 정도까지의 자료를 다루는 금융 관련 프로그램을 만들 때 유용합니다.

부동 소수점 숫자 형식 - C# 참조

코드 예제

//RealNumber.cs
using System;

namespace N_RealNumber
{
    internal class RealNumber
    {
        public void RealNumberEx()
        {
            float f = 99.99F;
            Console.WriteLine("float");
            Console.WriteLine(f + "\\n");

            double PI = 34.56D;
            Console.WriteLine("double");
            Console.WriteLine(PI+"\\n");

            decimal d = 12.34M;
            Console.WriteLine("decimal");
            Console.WriteLine(d + "\\n");
        }
    }
}
//Day2_Main.cs
using System;
using N_WriteLine;
using N_Placeholder;
using N_Value;
using N_Integer;
using N_RealNumber;

namespace Main
{
    class Day2_Main
    {
        static void Main(string[] args)
        {
            //WriteLine A = new WriteLine();
            //A.WriteEx();

            //Placeholder B = new Placeholder();
            //B.PlaceholderEx();

            //Value C = new Value();
            //C.ValueEx();

            //Integer D = new Integer();
            //D.IntegerEx();

            RealNumber E = new RealNumber();
            E.RealNumberEx();
        }
    }
}
728x90