Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created February 25, 2015 15:02
Show Gist options
  • Save ufcpp/013dd0997911b009c4f9 to your computer and use it in GitHub Desktop.
Save ufcpp/013dd0997911b009c4f9 to your computer and use it in GitHub Desktop.
String Interpolation with IFormattable
private static readonly IEnumerable<string> Cultures = new[] { "en-us", "fr", "zh-hk", "ja-jp" };
public static void Y()
{
var x = 10;
IFormattable f = $"{x :c}, {x :n}";
foreach (var c in Cultures)
{
var s = WithCulture(f, c);
Console.WriteLine(s);
}
}
public static void SameAsY()
{
var x = 10;
IFormattable f = System.Runtime.CompilerServices.FormattableStringFactory.Create("{0:c}, {1:n}", x, x);
foreach (var c in Cultures)
{
var s = WithCulture(f, c);
Console.WriteLine(s);
}
}
private static string WithCulture(IFormattable f, string cultureName)
{
return f.ToString(null, System.Globalization.CultureInfo.CreateSpecificCulture(cultureName));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment