使用欧洲数字系统的 .dll,但我的系统使用美国数字系统

Using .dll with European digit system, but my System uses American digit system

我正在重新制作我以前的练习考试,我们得到了一个欧洲 .dll 文件,其中包含一个简单的方法 returns 一个字符串,例如。 10,20;34,5;12,3;。问题出在欧洲而不是。对于点值,我们使用逗号。所以当我尝试解析它时,它没有给我正确的值或者根本不想解析。

// method.call() is just an example for calling the .dll method that returns the European format
double number;
if(!double.TryParse(method.call(), out number)) throw new ArgumentException("this is not a double");

我试图在 Console.WritLine() 中显示输出,它向我显示了带逗号的数字。所以我想这就是问题所在。因为当我用点数输入数字时它工作正常。

您可以使用此 TryParse 重载。通过传递用于解析的 CultureInfo,您可以切换到欧洲文化。

var value = "12,3";
var culture = CultureInfo.CreateSpecificCulture("de"); // german culture info
if (Double.TryParse(value, NumberStyles.Number, culture, out var number))
    Console.WriteLine("Converted '{0}' to {1}.", value, number);

value = "12.3";
culture = CultureInfo.CreateSpecificCulture("en-US"); // american culture info
if (Double.TryParse(value, NumberStyles.Number, culture, out number))
  Console.WriteLine("Converted '{0}' to {1}.", value, number);