How to use format strings with string interpolation in C#
By FoxLearn 3/20/2025 2:33:17 AM 33
Let’s see how to use format strings within an interpolated string:
decimal totalAmount = 15.745m; Console.WriteLine($"Your total is: {totalAmount:C}");
This will output:
Your total is: $15.75
This is equivalent to using string.Format()
:
string.Format("{0:C}", totalAmount);
The advantage of using string interpolation is that it is more concise and easier to read. You can use the same standard or custom format strings with both methods. The only difference is how you handle the culture.
Specifying the Culture
By default, interpolated strings use the current culture. If you need to specify a different culture, you can pass an IFormatProvider
using the string.Create()
method. For example, here's how to specify the en-GB
culture:
using System.Globalization; decimal totalAmount = 15.745m; var gbFormattedString = string.Create(CultureInfo.GetCultureInfo("en-GB"), $"Your total is: {totalAmount:C}"); Console.WriteLine(gbFormattedString);
This will output:
Your total is: £15.75
Older Versions of .NET (Before .NET 6)
The string.Create()
method was introduced in .NET 6. If you’re working with an older version of .NET, you can cast the interpolated string to a FormattableString
and use .ToString()
to apply a culture:
using System.Globalization; decimal totalAmount = 15.745m; // Using multiple lines FormattableString formattedString = $"(Multi-line) Your total is: {totalAmount:C}"; var formattedWithCulture = formattedString.ToString(CultureInfo.GetCultureInfo("en-GB")); Console.WriteLine(formattedWithCulture); // Using a single line Console.WriteLine(((FormattableString)$"(One-liner) Your total is: {totalAmount:C}").ToString(CultureInfo.GetCultureInfo("en-GB")));
This will output:
(Multi-line) Your total is: £15.75 (One-liner) Your total is: £15.75
The equivalent string.Format()
with culture would look like this:
string.Format(CultureInfo.GetCultureInfo("en-GB"), "Your total is: {0:C}", totalAmount);
Other Format Examples
1. Display a Number with 3 Decimal Places
To display only the last 3 decimal places of a number, use a custom format string:
decimal totalAmount = 15.745m; Console.WriteLine($"3 decimal places: {totalAmount:0.###}");
This will output:
3 decimal places: 15.745
2. Display the Current Time with Timezone Offset
You can format a DateTime
object to show the time along with the timezone offset:
DateTime currentTime = DateTime.Now; Console.WriteLine($"Current time: {currentTime:h:mm:ss tt K}");
This will output something like:
Current time: 8:13:12 AM -05:00
3. Format a US Phone Number
You can format a 10-digit US phone number using a custom format string:
long phoneNumber = 1234567890; Console.WriteLine($"Phone number: {phoneNumber:(###) ###-####}");
This will output:
Phone number: (123) 456-7890
- Primitive types in C#
- How to set permissions for a directory in C#
- How to Convert Int to Byte Array in C#
- How to Convert string list to int list in C#
- How to convert timestamp to date in C#
- How to Get all files in a folder in C#
- How to use Channel as an async queue in C#
- Case sensitivity in JSON deserialization