C# – How to use format strings with string interpolation

Interpolated strings have the following structure: {variable:format}. Typically you exclude the format, so they normally look like this: $”My name is {name}”.

Here’s how to use format strings with an interpolated string:

decimal orderAmount = 10.2322m;
Console.WriteLine($"You owe: {orderAmount:C}");
Code language: C# (cs)

This outputs the following:

You owe: $10.23Code language: plaintext (plaintext)

This is the equivalent of using string.Format() like this:

string.Format("{0:C}", orderAmount)
Code language: C# (cs)

Read more about why you should use string interpolation instead of string.Format().

As you can see, specifying a format string with an interpolated string is practically the same as how you’d do it with string.Format(). In fact, you can use all the same standard format strings and custom format strings. The only thing that’s a little bit different is how you specify the culture.

In this article, I’ll show more examples of using format strings with interpolated strings, including how to specify the culture.

Specifying the culture

Interpolated strings default to using the current culture. If you want to specify the culture, use the string.Create() overload that accepts an IFormatProvider. You can use this pass in the desired CultureInfo object (or whatever IFormatProvider you want to use). Here’s an example of how to do that:

using System.Globalization;

decimal orderAmount = 10.2322m;

var gbString = string.Create(CultureInfo.GetCultureInfo("en-GB"), $"You owe: {orderAmount:C}");

Console.WriteLine(gbString);
Code language: C# (cs)

This outputs the order amount formatted with the en-GB CultureInfo:

You owe: £10.23Code language: plaintext (plaintext)

Before .NET 6

The string.Create() overload shown above was added in .NET 6. If you’re using a version before .NET 6, you have to cast the interpolated string as a FormattableString, then call .ToString() and pass in the CultureInfo (or whatever IFormatProvider you want to use). This is quite verbose. You can do this with multiple lines or a dense one-liner, as shown below:

using System.Globalization;

decimal orderAmount = 10.2322m;

//multiple lines
FormattableString s = $"(Multi-liner) You owe: {orderAmount:C}";
var f = s.ToString(CultureInfo.GetCultureInfo("en-GB"));
Console.WriteLine(f);

//very dense 1 liner
Console.WriteLine(((FormattableString)$"(One-liner) You owe: {orderAmount:C}").ToString(CultureInfo.GetCultureInfo("en-GB")));
Code language: C# (cs)

This outputs the following:

(Multi-liner) You owe: £10.23
(One-liner) You owe: £10.23Code language: plaintext (plaintext)

string.Format() equivalent

Here’s the string.Format() equivalent of passing in the CultureInfo:

string.Format(CultureInfo.GetCultureInfo("en-GB"), "You owe: {0:C}", orderAmount)
Code language: C# (cs)

You may prefer the string.Format() approach if you’re using a version before .NET 6 because it’s less verbose. It’s about the same level of verbosity as the .NET 6 string.Create() approach.

Other format strings

You can use any standard format strings or custom format strings with string interpolation. I’ll show a few more examples below.

1 – Show only the last 3 decimal places

The following shows how to use a custom format string to show the last 3 decimals of a number:

decimal orderAmount = 10.2322m;
Console.WriteLine($"3 decimal places: {orderAmount:0.###}");
Code language: C# (cs)

This outputs the following:

3 decimal places: 10.232Code language: plaintext (plaintext)

2 – Show the current time, including the timezone offset

When you convert a DateTime to a string, you can specify the format you want to use. This example shows how to use a custom date/time format to show the time and timezone offset.

DateTime now = DateTime.Now;
Console.WriteLine($"Current time: {now:h:mm:ss tt K}");
Code language: C# (cs)

This outputs the following:

Current time: 8:13:12 AM -05:00Code language: plaintext (plaintext)

3 – Show a phone number in the United States format

This shows how to use a custom format string to display a 10-digit United States phone number (note: this is a fake phone number).

long phone = 5555555555;
Console.WriteLine($"Phone number: {phone:(###) ###-####}");
Code language: C# (cs)

This outputs the following:

Phone number: (555) 555-5555Code language: plaintext (plaintext)

Comments are closed.