Develop Dairy/C#

๋ฌธ์ž์—ด ์„œ์‹ ๊ธฐ๋ณธ ์‚ฌํ•ญ (String formatting basics) (2021. 02. 18)

ํ‰์ƒ์ฝ”๋”ฉ 2021. 2. 18. 21:18

๐Ÿ•ฎ๋ณตํ•ฉ ์„œ์‹ ์ง€์ •์ด๋ž€? (What is Composite Formatting?)

๋ณตํ•ฉ ์„œ์‹ ์ง€์ •์€ ๋ฌธ์ž์—ด ๋‚ด์—์„œ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„ ์ž๋ฆฌ ํ‘œ์‹œ์ž๋ฅผ ์‚ฌ์šฉ

๋Ÿฐํƒ€์ž„์— ์ค‘๊ด„ํ˜ธ๋กœ ๋ฌถ์€ ๋ชจ๋“  ํ•ญ๋ชฉ์€ ํ•ด๋‹น ์œ„์น˜์— ๋”ฐ๋ผ์„œ๋„ ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์œผ๋กœ ํ™•์ธ.

string ๋ฐ์ดํ„ฐ ํ˜•์‹ ํ‚ค์›Œ๋“œ์—, ๊ธฐ๋ณธ ์ œ๊ณต ๋ฉ”์„œ๋“œ Format()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ณตํ•ฉ ์„œ์‹ ์ง€์ •์˜ ์˜ˆ

string first = "Hello"; 

string second = "World"; 

string result = string.Format("{0} {1}!", first, second); 

๊ฒฐ๊ณผ

Hello World!

 

๐Ÿ•ฎ๋ฌธ์ž ๋ณด๊ฐ„์ด๋ž€? (What is string interpolation?)

๋ฌธ์ž์—ด๋ณด๊ฐ„์€ ๋ณตํ•ฉ ์„œ์‹ ์ง€์ •์„ ๊ฐ„์†Œํ™”ํ•˜๋Š” ์ƒˆ๋กœ์šด ๊ธฐ์ˆ 

์ผ๋ฐ˜์ ์œผ๋กœ ๋ณตํ•ฉ ์„œ์‹ ์ง€์ •๋ณด๋‹ค๋Š” ๋ฌธ์ž์—ด ๋ณด๊ฐ„์„ ์„ ํ˜ธ

๋ฌธ์ž์—ด์„ ๋ณด๊ฐ„ํ•˜๋ ค๋ฉด $ ์ง€์‹œ๋ฌธ ์•ž์— ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

string first = "Hello"; 

string second = "World"; 

Console.WriteLine($"{first} {second}!"); 

Console.WriteLine($"{second} {first}!"); 

Console.WriteLine($"{first} {first} {first}!");

๊ฒฐ๊ณผ

Hello World!

World Hello!

Hello Hello Hello!

 

๐Ÿ•ฎํ†ตํ™” ์„œ์‹ ์ง€์ • (Formatting currency)

๋ณตํ•ฉ ์„œ์‹ ์ง€์ • ๋ฐ ๋ฌธ์ž์—ด ๋ณด๊ฐ„์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์–ธ์–ด์™€ ๋ฌธํ™”๊ถŒ์— ๋Œ€ํ•ด ํ‘œ์‹œํ•  ๊ฐ’์˜ ์„œ์‹์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

:C ํ†ตํ™” ์„œ์‹ ์ง€์ •์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ price ๋ฐ discount ๋ณ€์ˆ˜๋ฅผ ํ†ตํ™”๋กœ ํ‘œ์‹œ

        decimal price = 123.45m;
        int discount = 50;
        Console.WriteLine($"Price: {price:C} (Save {discount:C})");

๊ฒฐ๊ณผ

Price: ¤123.45 (Save ¤50.00)

๊ตญ๊ฐ€ ํ™”ํ์— ๋Œ€ํ•œ ๊ธฐํ˜ธ ๋Œ€์‹  ๊ธฐํ˜ธ ¤๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ†ตํ™”์˜ ํ˜•์‹์— ๊ด€๊ณ„์—†์ด "ํ†ตํ™”"๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ผ๋ฐ˜ ๊ธฐํ˜ธ, .NET ํŽธ์ง‘๊ธฐ์—์„œ๋Š” ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฌด์‹œํ•˜๋ฏ€๋กœ ์ด ๊ธฐํ˜ธ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ Winodws ํ‘œ์‹œ ์–ธ์–ด๊ฐ€ ์˜์–ด๋กœ ์„ค์ •๋œ ๋ฏธ๊ตญ์˜ ์ปดํ“จํ„ฐ์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

Price: $123.45 (Save $50.00)

๋ฌธ์ž์—ด ์„œ์‹ ์ง€์ • ๊ธฐ๋Šฅ์˜ ์‚ฌ์šฉ์€ ์ปดํ“จํŒ… ๋ฌธํ™”๊ถŒ์— ๋”ฐ๋ผ ์ขŒ์šฐ๋˜๊ธฐ ๋•Œ๋ฌธ์—, "๋ฌธํ™”๊ถŒ"์ด๋ผ๋Š” ์šฉ์–ด๋Š” ์ตœ์ข… ์‚ฌ์šฉ์ž์˜ ๊ตญ๊ฐ€ ๋ฐ ์–ธ์–ด๋ฅผ ๋‚˜ํƒ€๋ƒ„

 

๐Ÿ•ฎ์ˆซ์ž ์„œ์‹ ์ง€์ • (Formatting numbers)

์ˆซ์ž ๋ฐ์ดํ„ฐ๋กœ ์ž‘์—…ํ•  ๋•Œ ์‰ผํ‘œ๋กœ ์ฒœ ๋‹จ์œ„, ๋ฐฑ๋งŒ ๋‹จ์œ„, ์‹ญ์–ต ๋‹จ์œ„ ๋“ฑ์„ ๋‚˜ํƒ€๋‚ด ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆซ์ž์˜ ์„œ์‹์„ ์ง€์ • ํ•  ์ˆ˜ ์žˆ๋‹ค.

decimal measurement = 123456.78912m; 

Console.WriteLine($"Measurement: {measurement:4N} units");

N ์ˆซ์ž ์„œ์‹ ์ง€์ •์ž๋Š” ์†Œ์ˆ˜์  ๋’ค์— 2๊ฐœ์˜ ์ˆซ์ž๋งŒ ํ‘œ์‹œ ex)N4๋Š” ์†Œ์ˆ˜์  ๋’ค์— 4์ž๋ฆฌ

Measurement: 123,456.7891 units

 

๐Ÿ•ฎ๋ฐฑ๋ถ„์œจ ์„œ์‹ ์ง€์ • (Formatting percentages)

๋ฐฑ๋ถ„์œจ ์„œ์‹์„ ์ง€์ •ํ•˜๋ ค๋ฉด P ์„œ์‹ ์ง€์ •์ž๋ฅผ ์‚ฌ์šฉ, ์†Œ์ˆ˜์  ๋’ค์— ํ‘œ์‹œ๋˜๋Š” ๊ฐ’์˜ ์ˆ˜๋ฅผ ์ œ์–ด

decimal tax = .36785m; 

Console.WriteLine($"Tax rate: {tax:P2}");

Tax rate: 36.79 %

int invoiceNumber = 1201;
decimal productMeasurement = 25.4568m;
decimal subtotal = 2750.00m;
decimal taxPercentage = .15825m;
decimal total = 3185.19m;

Console.WriteLine($"Invoice Number: {invoiceNumber}");
Console.WriteLine($"   Measurement: {productMeasurement:N3} mg");
Console.WriteLine($"     Sub Total: {subtotal:C}");
Console.WriteLine($"           Tax: {taxPercentage:P2}");
Console.WriteLine($"     Total Due: {total:C}");

Invoice Number: 1201

Measurement: 25.457 mg

Sub Total: ¤2,750.00

Tax: 15.83 %

Total Due: ¤3,185.19

๐Ÿ•ฎ์•ˆ์ชฝ ์—ฌ๋ฐฑ ๋ฐ ๋งž์ถค (padding and alignment)

์•ž ๋˜๋Š” ๋’ค์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž์—ด ์„œ์‹ ์ง€์ •

string input = "Pad this"; 

Console.WriteLine(input.PadLeft(12));

 

โ€‹ Pad this

 

Console.WriteLine(input.PadLeft(12, '-')); 

Console.WriteLine(input.PadRight(12, '-'));

 

----Pad this

Pad this----

 

 

์ถœ์ฒ˜: Ms Docs Learn C#์œผ๋กœ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์˜์ˆซ์ž ๋ฐ์ดํ„ฐ ์„œ์‹ ์ง€์ •