상세 컨텐츠

본문 제목

string.Format

C#

by 메타 스토리 2023. 9. 5. 02:14

본문

int quantity = 12345;
string formatted = string.Format("Quantity: {0:N0}", quantity);
// 결과: "Quantity: 12,345"

 

double price = 19.99;
string formatted = string.Format("Price: {0:F2}", price);
// 결과: "Price: 19.99"

 

double discount = 0.15;
string formatted = string.Format("Discount: {0:P}", discount);
// 결과: "Discount: 15.00%"

 

decimal rate = 0.035m;
string formatted = string.Format("Interest Rate: {0:F3}", rate);
// 결과: "Interest Rate: 0.035"

 

double distance = 12345678;
string formatted = string.Format("Distance: {0:E}", distance);
// 결과: "Distance: 1.234568E+07"

 

int value = 255;
string formatted = string.Format("Hex Value: {0:X}", value);
// 결과: "Hex Value: FF"

 

decimal amount = 12345.67m;
string formatted = string.Format("Amount: {0:C}", amount);
// 결과: "Amount: $12,345.67"

 

int population = 1234567890;
string formatted = string.Format("Population: {0:N}", population);
// 결과: "Population: 1,234,567,890"

 

int temperature = -15;
string formatted = string.Format("Temperature: {0:+#;-#;0}°C", temperature);
// 결과: "Temperature: -15°C"

 

int score = 42;
string formatted = string.Format("Score: {0,5}", score);
// 결과: "Score:    42"

 

int value = 255;
string formatted = string.Format("Hex Value: 0x{0:X4}", value);
// 결과: "Hex Value: 0x00FF"

 

DateTime now = DateTime.Now;
string formatted = string.Format("Current date and time: {0:yyyy-MM-dd HH:mm:ss}", now);
// 결과: "Current date and time: 2023-09-04 14:30:00"

 

DateTime date = new DateTime(2023, 9, 4);
string formatted = string.Format("Custom format: {0:dddd, MMMM dd, yyyy}", date);
// 결과: "Custom format: Monday, September 04, 2023"

 

 

string formatted = string.Format("Name: {0}, Age: {1}, City: {2}", "John", 30, "New York");
// 결과: "Name: John, Age: 30, City: New York"

 

double value = 1234.56;
string formatted = string.Format(System.Globalization.CultureInfo.CurrentCulture, "Value: {0:C}", value);
// 결과: "Value: $1,234.56" (문화권에 따라 다름)

 

string name = "Alice";
string formatted = string.Format("Hello, {0}!", name);
// 결과: "Hello, Alice!"

 

string text = "Hello";
string formatted = string.Format("{0,-10}", text); // 왼쪽 정렬
// 결과: "Hello     "

 

string name = "Alice";
string action = "welcome";
string formatted = string.Format("Hello, {0}, we {1} you!", name, action);
// 결과: "Hello, Alice, we welcome you!"

 

int age = 30;
string formatted = string.Format("I am {0:D} years old.", age);
// 결과: "I am 30 years old."

 

double price = 19.99;
string formatted = string.Format("The price is {0:C2}", price);
// 결과: "The price is $19.99"

 

DateTime now = DateTime.Now;
string formatted = string.Format("Current date and time: {0:yyyy-MM-dd HH:mm:ss}", now);
// 결과: "Current date and time: 2023-09-04 15:30:00"

 

double percentage = 0.75;
string formatted = string.Format("The percentage is {0:P0}", percentage);
// 결과: "The percentage is 75%"

 

double value = 123.45678;
string formatted = string.Format("Value: {0:F2}", value);
// 결과: "Value: 123.46"

 

double number = 123456789.0;
string formatted = string.Format("Scientific notation: {0:E}", number);
// 결과: "Scientific notation: 1.234568E+08"

 

string code = "A1";
string formatted = string.Format("Code: {0,-5}", code);
// 결과: "Code: A1   "

 

DateTime date = new DateTime(2023, 9, 4);
string formatted = string.Format("Short date: {0:d}", date);
// 결과: "Short date: 9/4/2023"

 

int number = 42;
string formatted = string.Format("Answer: {0:0000}", number);
// 결과: "Answer: 0042"

 

 

 

'C#' 카테고리의 다른 글

Stack  (0) 2023.09.05
Queue  (0) 2023.09.05
List  (0) 2023.09.05
문자열  (0) 2023.09.05
Path  (0) 2023.09.04

관련글 더보기