반응형
    
    
    
  사용자 지정 숫자 형식 문자

표준 숫자 서식 문자

소수점 두번째까지만 출력하는 방법 예시
1. ToString
01 pi.ToString("0.00")
double pi = 3.14159265359;
string result = pi.ToString("0.00");
Console.WriteLine($"pi.ToString 결과 : {result}");

02 pi.ToString("n2")
double pi = 3.14159265359;
string result = pi.ToString("n2");
Console.WriteLine($"pi.ToString 결과 : {result}");

2. String.Format
01 String.Format("{0:N2}", pi)
double pi = 3.14159265359;
string result = String.Format("{0:N2}", pi);
Console.WriteLine($"String.Format{0:N2} 결과 : {result}");

02 String.Format("{0:0.00}", pi)
double pi = 3.14159265359;
string result = String.Format("{0:0.00}", pi);
 Console.WriteLine($"String.Format{0:0.00} 결과 : {result}");

3. Math.Round
01 Math.Round(pi,2)
double pi = 3.14159265359;
double result = Math.Round(pi, 2);
Console.WriteLine($"Math.Round 결과 : {result}");
반응형
    
    
    
  '코딩테스트 준비 > 자료구조 & 알고리즘' 카테고리의 다른 글
| C# - Math.Pow(제곱 연산, 제곱근 연산) (0) | 2023.05.30 | 
|---|---|
| C# - 2차원 배열 (1) | 2023.05.15 | 
| C# - 배열 연결, 덧붙이는 방법 (Enumerable.Concat() 사용) (0) | 2023.05.03 | 
| C# - 아스키코드(정수를 문자로, 문자를 정수로) (0) | 2023.04.26 | 
| C# - 중복된 값 제거하기 (0) | 2023.04.18 | 
										
									
										
									
										
									
										
									
댓글