Tip #008
Culture-aware interpolation
About this tip
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 coffeeCode
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.00100,00 €