【问题标题】:Using .dll with European digit system, but my System uses American digit system使用带有欧洲数字系统的 .dll,但我的系统使用美国数字系统
【发布时间】:2019-08-25 08:44:56
【问题描述】:

我正在重新制作我之前的考试以进行练习,我们得到了一个欧洲 .dll 文件,其中包含一个返回字符串的简单方法,例如。 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() 中显示输出,它向我显示了带逗号的数字。所以我想这就是问题所在。因为当我输入带有点的数字时,它可以正常工作。

【问题讨论】:

    标签: c# decimal point comma


    【解决方案1】:

    您可以使用此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);
    

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2018-07-30
      • 2014-10-06
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多