От: | Sinix | ||
Дата: | 18.03.16 08:40 | ||
Оценка: |
public static class InvariantString
{
public static CultureInfo Culture => CultureInfo.InvariantCulture;
public static string ToInvariantString<T>(this T s) where T : IFormattable => s.ToString(null, Culture);
public static string ToInvariantString<T>(this T s, string format) where T : IFormattable => s.ToString(format, Culture);
// ...
public static double ParseDouble(string s) => double.Parse(s, Culture);
}
public class Program
{
static void Main(string[] args)
{
Right();
Wrong();
}
private static void Wrong()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var x = 12.3.ToString();
// ...
Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
double y = double.Parse(x); // FAIL
Console.WriteLine(y);
}
private static void Right()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var x = 12.3.ToInvariantString();
// ...
Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");
double y = InvariantString.ParseDouble(x); // OK
Console.WriteLine(y);
}
}
Спойлеры! | |
.Contains() .Replace() .Equals() дальше сами | |