skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Culture-aware interpolation

C# 6

Code card: Culture-aware interpolation. Program output: $100.00 / 100,00 €

FormattableString keeps the values and :C format for rendering with a chosen culture. The fr-FR result has a non-breaking space before €. Interpolate once, format later: a FormattableString keeps the :C format so you render it with any CultureInfo. It is available since C# 6. The key line is FormattableString text = $"{price:C}" in formats.cs. Run it and the output reads $100.00, 100,00 €.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
formats.cs
using System.Globalization;
var price = 100m;
FormattableString text = $"{price:C}";
var usa = CultureInfo.GetCultureInfo("en-US");
var euro = CultureInfo.GetCultureInfo("fr-FR");
Console.WriteLine(text.ToString(usa));
Console.WriteLine(text.ToString(euro));
Output
$ dotnet run formats.cs
$100.00
100,00 €