String formatting is an essential part of C# programming because applications constantly need to display, combine, and organize text and values. Whether you are creating a console application, web application, desktop program, or API, knowing how to format strings makes your code cleaner and your output easier to understand.
C# provides several ways to format strings, including composite formatting, string interpolation, formatted strings, and the String.Format() method. Each approach has its own advantages and can be useful in different programming situations.
In this guide, we will explore practical C# string formatting examples, explain how each method works, and show when you should use one approach over another.
What Is String Formatting in C#?
String formatting means combining text with values and presenting those values in a specific format.
For example, suppose you have a user’s name and age:
string name = "John";
int age = 25;
You may want to display:
John is 25 years old.
Instead of manually joining multiple strings, C# provides formatting techniques that allow you to create readable output more efficiently.
String formatting can be used with:
- Strings
- Integers
- Decimal numbers
- Dates and times
- Currency values
- Percentages
- Custom numeric formats
- Objects and other data types
Why String Formatting Is Important
Proper string formatting improves both the readability of your application and the quality of its output.
For example, a poorly formatted message might look like this:
Customer:JohnAge:30Balance:1250.50
With proper formatting, the same information becomes much easier to understand:
Customer: John
Age: 30
Balance: $1,250.50
String formatting is particularly useful when building:
- User interfaces
- Reports
- Receipts
- Invoices
- Log messages
- Console applications
- API responses
- Error messages
- Database-related applications
1. Using String Concatenation
One of the simplest ways to combine strings in C# is string concatenation.
The + operator can be used to join text and variables.
string name = "Alice";
int age = 28;
string message = "My name is " + name + " and I am " + age + " years old.";
Console.WriteLine(message);
Output:
My name is Alice and I am 28 years old.
String concatenation is easy to understand and works well for simple expressions.
However, when a string contains many variables, concatenation can become difficult to read.
For example:
string message = "Name: " + name + ", Age: " + age + ", City: " + city;
For more complex formatting, string interpolation is usually cleaner.
2. Using String Interpolation
String interpolation is one of the most convenient ways to format strings in modern C#.
It uses the $ symbol before the string.
string name = "Sarah";
int age = 30;
string message = $"My name is {name} and I am {age} years old.";
Console.WriteLine(message);
Output:
My name is Sarah and I am 30 years old.
The expressions inside {} are evaluated and inserted directly into the string.
Why Use String Interpolation?
String interpolation makes code easier to read because the variables appear exactly where their values will be displayed.
Compare:
"Hello " + name + ", your balance is " + balance
with:
$"Hello {name}, your balance is {balance}"
The second version is generally easier to understand.
3. Formatting Numbers With Interpolation
String interpolation also supports format specifiers.
For example, you can display a decimal value with two decimal places:
double price = 49.5;
Console.WriteLine($"Price: {price:F2}");
Output:
Price: 49.50
The F2 format specifies two digits after the decimal point.
Other useful numeric formats include:
| Format | Description | Example |
|---|---|---|
F2 | Two decimal places | 25.50 |
N2 | Number with separators | 1,250.50 |
C2 | Currency | $1,250.50 |
P2 | Percentage | 25.00% |
4. Formatting Currency
Currency formatting is common in shopping carts, invoices, accounting software, and financial applications.
You can use the C format specifier:
decimal price = 1250.75m;
Console.WriteLine($"Price: {price:C2}");
The exact currency symbol depends on the current culture settings.
A typical output could be:
Price: $1,250.75
You can also use String.Format() for the same purpose:
decimal price = 1250.75m;
string result = String.Format("Price: {0:C2}", price);
Console.WriteLine(result);
5. Using String.Format()
Before string interpolation became popular, String.Format() was one of the standard approaches for formatting strings.
The basic syntax is:
String.Format("text {0}", value);
For example:
string name = "David";
int age = 35;
string result = String.Format("Name: {0}, Age: {1}", name, age);
Console.WriteLine(result);
Output:
Name: David, Age: 35
The {0} refers to the first value, while {1} refers to the second value.
Multiple Values
You can format several values at once:
string name = "Emma";
int age = 27;
string city = "London";
string result = String.Format(
"Name: {0}, Age: {1}, City: {2}",
name,
age,
city
);
This approach is still useful when working with existing codebases or APIs that expect composite formatting.
6. Formatting Dates in C#
Dates often need to be displayed in a specific format.
For example:
DateTime date = DateTime.Now;
Console.WriteLine($"Today is {date:yyyy-MM-dd}");
A possible output is:
Today is 2026-09-11
You can use different date patterns depending on your requirements.
Common examples include:
Console.WriteLine($"{date:dd/MM/yyyy}");
Console.WriteLine($"{date:MM/dd/yyyy}");
Console.WriteLine($"{date:yyyy-MM-dd}");
This makes it easier to create consistent date displays in applications.
7. Formatting Time Values
You can also format the time portion of a DateTime value.
DateTime now = DateTime.Now;
Console.WriteLine($"Current time: {now:HH:mm:ss}");
Output:
Current time: 19:45:30
Common time formatting patterns include:
HH:mm— 24-hour hours and minutesHH:mm:ss— hours, minutes, and secondshh:mm tt— 12-hour format with AM/PM
For example:
Console.WriteLine($"Time: {now:hh:mm tt}");
8. Formatting Percentages
Percentage formatting is useful when displaying statistics, scores, growth rates, and performance measurements.
double completion = 0.875;
Console.WriteLine($"Completion: {completion:P1}");
Output:
Completion: 87.5%
The P1 format displays the value as a percentage with one decimal place.
Similarly:
double progress = 0.7565;
Console.WriteLine($"{progress:P2}");
Output:
75.65%
9. Controlling Decimal Places
Sometimes you need a specific number of decimal places.
For example:
double temperature = 24.67891;
Console.WriteLine($"Temperature: {temperature:F2}°C");
Output:
Temperature: 24.68°C
This is useful when displaying:
- Prices
- Measurements
- Scores
- Scientific values
- Calculations
- Statistical results
10. Adding Thousands Separators
Large numbers can be difficult to read without separators.
For example:
int population = 1250000;
Console.WriteLine($"Population: {population:N0}");
Output:
Population: 1,250,000
You can also specify decimal places:
double revenue = 1250345.678;
Console.WriteLine($"Revenue: {revenue:N2}");
Output:
Revenue: 1,250,345.68
11. Using Alignment in String Formatting
C# also allows you to control the alignment of formatted values.
For example:
string name = "Alice";
Console.WriteLine($"|{name,10}|");
The value is aligned within a field of 10 characters.
You can also use negative values for left alignment:
Console.WriteLine($"|{name,-10}|");
Alignment can be useful when creating simple console tables.
For example:
Console.WriteLine($"{"Name",-15} {"Age",5}");
Console.WriteLine($"{"Alice",-15} {25,5}");
Console.WriteLine($"{"Michael",-15} {31,5}");
This can produce a more organized console output.
12. Custom Numeric Formatting
C# supports custom numeric format strings when standard formats do not provide enough control.
For example:
int number = 12345;
Console.WriteLine($"{number:#,##0}");
Output:
12,345
You can also create more specific patterns depending on the application’s requirements.
Custom numeric formatting is useful when displaying business data that needs a particular presentation style.
13. Formatting Strings With Multiple Variables
A common real-world requirement is creating a message containing several different types of values.
For example:
string product = "Laptop";
decimal price = 899.99m;
int quantity = 2;
string message =
$"Product: {product}, Price: {price:C2}, Quantity: {quantity}";
Console.WriteLine(message);
Output:
Product: Laptop, Price: $899.99, Quantity: 2
String interpolation makes this type of formatting straightforward and readable.
14. Conditional Expressions Inside Interpolated Strings
C# allows expressions inside interpolated strings.
For example:
int age = 20;
string message = $"Status: {(age >= 18 ? "Adult" : "Minor")}";
Console.WriteLine(message);
Output:
Status: Adult
This can be useful for small formatting decisions.
However, complicated business logic should generally remain outside the string to keep the code easy to maintain.
15. Escaping Braces in Interpolated Strings
Curly braces have a special meaning inside interpolated strings because they indicate expressions.
If you need to display literal braces, you can escape them by using double braces.
string text = $"{{Hello, {name}}}";
Console.WriteLine(text);
The double braces tell C# that the braces should be treated as literal characters.
C# String Interpolation vs String.Format()
Both approaches can format strings, but their syntax is different.
String.Format()
string result = String.Format(
"Hello {0}, your age is {1}.",
name,
age
);
String Interpolation
string result = $"Hello {name}, your age is {age}.";
For modern C# applications, interpolation is often easier to read because the variables are directly embedded in the string.
However, String.Format() remains important for understanding older C# code and composite formatting.
Best Practices for C# String Formatting
Following a few simple practices can make your code cleaner and easier to maintain.
Prefer Interpolation for Readable Code
When you are working with modern C#, string interpolation is often a clear choice:
string message = $"Welcome, {username}!";
Use Appropriate Format Specifiers
Choose the correct format for the type of data you are displaying.
For example:
decimal price = 99.99m;
Console.WriteLine($"{price:C2}");
Avoid Unnecessary Concatenation
Long chains of + operators can make code harder to read.
Instead of:
string message = "Name: " + name + ", Age: " + age + ", City: " + city;
consider:
string message = $"Name: {name}, Age: {age}, City: {city}";
Consider Culture
Currency, dates, numbers, and percentages can vary depending on culture and regional settings. Applications that serve international users should carefully consider culture-specific formatting.
Keep Complex Logic Outside Strings
Although interpolated strings support expressions, avoid placing complicated calculations or business rules inside them.
Instead:
decimal total = price * quantity;
string message = $"Total: {total:C2}";
This is generally easier to read than embedding the entire calculation inside the string.
Common C# String Formatting Mistakes
Forgetting the $ Symbol
This will not perform interpolation:
string message = "Hello {name}";
You need:
string message = $"Hello {name}";
Using the Wrong Format Specifier
A format specifier should match the type and desired output.
For example, C2 is useful for currency, while P2 is designed for percentages.
Mixing Too Many Formatting Techniques
Using several different approaches unnecessarily can make a project inconsistent. Choose a formatting style that fits your project’s coding standards and use it consistently.
Ignoring Culture-Specific Output
A date or currency value that looks correct in one region may appear differently in another. Applications serving international audiences should handle culture appropriately.
Frequently Asked Questions
What is string formatting in C#?
String formatting is the process of combining text and values while controlling how those values are displayed.
What is the easiest way to format strings in C#?
For modern C# applications, string interpolation is usually one of the easiest and most readable options.
How do I format currency in C#?
You can use the C format specifier:
decimal amount = 2500.50m;
Console.WriteLine($"{amount:C2}");
How do I format a date in C#?
Use a date format pattern inside an interpolated string:
DateTime date = DateTime.Now;
Console.WriteLine($"{date:yyyy-MM-dd}");
What does F2 mean in C#?
F2 formats a numeric value as a fixed-point number with two digits after the decimal point.
What does N2 mean?
N2 formats a number using thousands separators and two decimal places.
Is string interpolation better than String.Format()?
String interpolation is often easier to read in modern C# code, while String.Format() remains useful for composite formatting and existing applications.
Conclusion
Understanding C# string formatting examples is important for developers who want to create clean, readable, and professional applications. C# provides several formatting techniques, including string concatenation, String.Format(), string interpolation, numeric format specifiers, date formatting, currency formatting, percentage formatting, and custom formats.
For most modern applications, string interpolation provides a simple and readable way to combine values with text. However, understanding String.Format() and standard format specifiers is also valuable because these techniques remain common across many C# projects.
By choosing the right formatting approach and using appropriate format specifiers, you can create clearer console output, reports, messages, invoices, logs, and user interfaces while keeping your C# code easy to maintain.
