Created
February 25, 2015 15:02
-
-
Save ufcpp/013dd0997911b009c4f9 to your computer and use it in GitHub Desktop.
String Interpolation with IFormattable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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