티스토리 뷰

IT지식

c# String.Format 사용

민자르 2018. 4. 18. 00:26
반응형

String.Format 메서드를 사용하거나 String.Format을 호출하는 Console.Write 또는 Console.WriteLine 메서드를 통하거나 문자열 보간을 사용하여 숫자 결과의 서식을 지정할 수 있습니다. 형식은 형식 문자열을 사용하여 지정됩니다. 다음 표에는 지원되는 표준 형식 문자열을 보여 줍니다. 형식 문자열은 Axx 형식을 사용합니다. 여기서 A는 형식 지정자이고 xx는 전체 자릿수 지정자입니다. 형식 지정자는 숫자 값에 적용되는 서식 형식을 제어하고, 전체 자릿수 지정자는 서식이 지정된 출력의 유효 자릿수 또는 소수 자릿수를 제어합니다. 전체 자릿수 지정자의 값은 0에서 99 사이입니다.

표준 및 사용자 지정 서식 문자열에 대한 자세한 내용은 형식 서식 지정을 참조하세요.



Decimal pricePerOunce = 17.36m; String s = String.Format("The current price is {0} per ounce.", pricePerOunce); Console.WriteLine(s); // Result: The current price is 17.36 per ounce.



Decimal pricePerOunce = 17.36m; String s = String.Format("The current price is {0:C2} per ounce.", pricePerOunce); Console.WriteLine(s); // Result if current culture is en-US: // The current price is $17.36 per ounce.



decimal temp = 20.4m; string s = String.Format("The temperature is {0}°C.", temp); Console.WriteLine(s); // Displays 'The temperature is 20.4°C.'



string s = String.Format("At {0}, the temperature is {1}°C.", DateTime.Now, 20.4); Console.WriteLine(s); // Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'



string s = String.Format("It is now {0:d} at {0:t}", DateTime.Now); Console.WriteLine(s); // Output similar to: 'It is now 4/10/2015 at 10:04 AM'



using System; using System.Text; class Example { public static void Main() { // <Snippet33> int[] years = { 2013, 2014, 2015 }; int[] population = { 1025632, 1105967, 1148203 }; var sb = new StringBuilder(); sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population")); for (int index = 0; index < years.Length; index++) sb.Append(String.Format("{0,6} {1,15:N0}\n", years[index], population[index])); Console.WriteLine(sb); } } // Result: // Year Population // // 2013 1,025,632 // 2014 1,105,967 // 2015 1,148,203



int[] years = { 2013, 2014, 2015 }; int[] population = { 1025632, 1105967, 1148203 }; String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population"); for(int index = 0; index < years.Length; index++) s += String.Format("{0,-10} {1,-10:N0}\n", years[index], population[index]); Console.WriteLine($"\n{s}"); // Result: // Year Population // // 2013 1,025,632 // 2014 1,105,967 // 2015 1,148,203



DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0); string city = "Chicago"; int temp = -16; string output = String.Format("At {0} in {1}, the temperature was {2} degrees.", dat, city, temp); Console.WriteLine(output); // The example displays output like the following: // At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.



using System; public class Example { public static void Main() { // Create array of 5-tuples with population data for three U.S. cities, 1940-1950. Tuple<string, DateTime, int, DateTime, int>[] cities = { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277, new DateTime(1950, 1, 1), 1970358), Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995, new DateTime(1950, 1, 1), 7891957), Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808, new DateTime(1950, 1, 1), 3620962), Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452, new DateTime(1950, 1, 1), 1849568) }; // Display header var header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", "City", "Year", "Population", "Change (%)"); Console.WriteLine(header); foreach (var city in cities) { var output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}", city.Item1, city.Item2, city.Item3, city.Item4, city.Item5, (city.Item5 - city.Item3)/ (double)city.Item3); Console.WriteLine(output); } } } // The example displays the following output: // City Year Population Year Population Change (%) // // Los Angeles 1940 1,504,277 1950 1,970,358 31.0 % // New York 1940 7,454,995 1950 7,891,957 5.9 % // Chicago 1940 3,396,808 1950 3,620,962 6.6 % // Detroit 1940 1,623,452 1950 1,849,568 13.9 %


형식 지정자설명예제출력
C 또는 c통화Console.Write("{0:C}", 2.5);

Console.Write("{0:C}", -2.5);
$2.50

($2.50)
D 또는 dDecimalConsole.Write("{0:D5}", 25);00025
E 또는 e지수Console.Write("{0:E}", 250000);2.500000E+005
F 또는 f고정 소수점Console.Write("{0:F2}", 25);

Console.Write("{0:F0}", 25);
25.00

25
G 또는 g일반Console.Write("{0:G}", 2.5);2.5
N 또는 nConsole.Write("{0:N}", 2500000);2,500,000.00
X 또는 x16진수Console.Write("{0:X}", 250);

Console.Write("{0:X}", 0xffff);
FA

FFFF


728x90
반응형
댓글