Code4IT

The place for .NET enthusiasts, Azure lovers, and backend developers

C# Tip: Format Interpolated Strings

2023-05-02 3 min read CSharp Tips

Interpolated strings are those built with the $ symbol, that you can use to create strings using existing variables or properties. Did you know that you can apply custom formattings to such values?

Table of Contents

Just a second! 🫷
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .

If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

Thank you for your understanding.
- Davide

As you know, there are many ways to “create” strings in C#. You can use a StringBuilder, you can simply concatenate strings, or you can use interpolated strings.

Interpolated? WHAT? I’m pretty sure that you’ve already used interpolated strings, even if you did not know the “official” name:

int age = 31;
string bio = $"Hi, I'm {age} years old";

That’s it: an interpolated string is one where you can reference a variable or a property within the string definition, using the $ and the {} operators to generate such strings.

Did you know that you can even format how the interpolated value must be rendered when creating the string? It’s just a matter of specifying the format after the : sign:

Formatting dates

The easiest way to learn it is by formatting dates:

DateTime date = new DateTime(2021,05,23);

Console.WriteLine($"The printed date is {date:yyyy-MM-dd}"); //The printed date is 2021-05-23
Console.WriteLine($"Another version is {date:yyyy-MMMM-dd}"); //Another version is 2021-May-23

Console.WriteLine($"The default version is {date}"); //The default version is 23/05/2021 00:00:00

Here we have date:yyyy-MM-dd which basically means “format the date variable using the yyyy-MM-dd format”.

There are, obviously, different ways to format dates, as described on the official documentation. Some of the most useful are:

  • dd: day of the month, in number (from 01 to 31);
  • ddd: abbreviated day name (eg: Mon)
  • dddd: complete day name (eg: Monday)
  • hh: hour in a 12-hour clock (01-> 12)
  • HH: hour in a 24-hour clock (00->23)
  • MMMM: full month day

and so on.

Formatting numbers

Similar to dates, we can format numbers.

For example, we can format a double number as currency or as a percentage:

var cost = 12.41;
Console.WriteLine($"The cost is {cost:C}"); // The cost is Β£12.41

var variation = -0.254;
Console.WriteLine($"There is a variation of {variation:P}"); //There is a variation of -25.40%

Again, there are lots of different ways to format numbers:

  • C: currency - it takes the current culture, so it may be Euro, Yen, or whatever currency, depending on the process’ culture;
  • E: exponential number, used for scientific operations
  • P: percentage: as we’ve seen before {1:P} represents 100%;
  • X: hexadecimal

Further readings

There are too many formats that you can use to convert a value to a string, and we cannot explore all of them here.

But still, you can have a look at several ways to format date and time in C#

πŸ”— Custom date and time format strings | Microsoft Docs

and, obviously, to format numbers

πŸ”— Standard numeric format strings | Microsoft Docs

This article first appeared on Code4IT 🐧

Finally, remember that interpolated strings are not the only way to build strings upon variables; you can (and should!) use string.Format:

πŸ”— How to use String.Format - and why you should care about it | Code4IT

Wrapping up

I hope you enjoyed this article! Let’s keep in touch on Twitter or on LinkedIn, if you want! πŸ€œπŸ€›

Happy coding!

🐧

About the author

Davide Bellone is a Principal Backend Developer with more than 10 years of professional experience with Microsoft platforms and frameworks.

He loves learning new things and sharing these learnings with others: that’s why he writes on this blog and is involved as speaker at tech conferences.

He's a Microsoft MVP πŸ†, conference speaker (here's his Sessionize Profile) and content creator on LinkedIn.