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.23
Code language: plaintext (plaintext)

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

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

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, you have to cast the interpolated string as a FormattableString, then call .ToString() on it and pass in the culture.

This is quite verbose. You can either split it into multiple lines or have a single, very-dense one-liner, as follows:

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

This outputs the following:

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

This is equivalent to the following string.Format() approach:

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

You may prefer the string.Format() approach here. It’s less verbose, but you do lose the benefits of string interpolation.

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.232
Code language: plaintext (plaintext)

2 – Show the current time, including the timezone offset

This shows how to use a custom date/time format string 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:00
Code 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-5555
Code language: plaintext (plaintext)

Leave a Comment