본문 바로가기
프로그래밍/C#

[c#] 003-형식지정자, 변수 선언과 자료형(2)

by 플로어코딩 2020. 1. 20.

C#에서 형식지정자를 알아보도록 하겠습니다.

 

통화 구분기호로 표시를 할 경우 형식지정자를 사용해야하는 경우가 생기는데요.

백문이불여일견!

소스를 보는 것이 좀 더 이해가 빠르겠죠?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A010_ConsoleFormat
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.Clear();

      Console.WriteLine("Standard Numeric Format Specifiers");
      Console.WriteLine(
          "(C) Currency: . . . . . . . . {0:C}\n" +
          "(D) Decimal:. . . . . . . . . {0:D}\n" +
          "(E) Scientific: . . . . . . . {1:E}\n" +
          "(F) Fixed point:. . . . . . . {1:F}\n" +
          "(G) General:. . . . . . . . . {0:G}\n" +
          "(N) Number: . . . . . . . . . {0:N}\n" +
          "(P) Percent:. . . . . . . . . {1:P}\n" +
          "(R) Round-trip: . . . . . . . {1:R}\n" +
          "(X) Hexadecimal:. . . . . . . {0:X}\n",
          -12345678, -1234.5678f);
    }
  }
}

 

결과

 

통화 형식지정자를 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A012_FloatDoubleDecimal
{
  class Program
  {
    static void Main(string[] args)
    {
      float flt = 1F / 3;
      double dbl = 1D / 3;
      decimal dcm = 1M / 3;
      Console.WriteLine("float: {0}\ndouble: {1}\ndecimal: {2}", flt, dbl, dcm);
      Console.WriteLine("float: {0} bytes\ndouble: {1} bytes\ndecimal: {2} bytes",
        sizeof(float), sizeof(double), sizeof(decimal));
      Console.WriteLine("float : {0}~{1}", float.MinValue, float.MaxValue);
      Console.WriteLine("double : {0}~{1}", double.MinValue, double.MaxValue);
      Console.WriteLine("decimal : {0}~{1}", decimal.MinValue, decimal.MaxValue);

      Console.WriteLine("float : {0}", float.MaxValue+1);
      Console.WriteLine("double : {0}", double.MaxValue + 1);
     }
  }
}

각각 float, double, decimal 의 바이트 크기도 sizeof() 메소드를 이용하여 출력해보았다.

 

 

 

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

[c#] 002-입력받기, 변수 선언과 자료형(1)  (0) 2020.01.19
[C#] visual studio C# 시작하기  (0) 2020.01.19

댓글