Search code examples
c#internationalizationnumber-formattingdecimal-point

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


I'm remaking my previous exam for exercise, we got an European .dll file with a simple method that returns a string eg. 10,20;34,5;12,3; . The problem is in Europe instead of . for point values, we use a comma. So when I try to parse this it doesn't give me correct values or doesn't want to parse at all.

// 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");

I tried to show the output instead in Console.WritLine() and it showed me the digit with commas. So I guess this is the problem. Because when I enter digits with points it works fine.


Solution

  • You can use this TryParse overload. By passing the CultureInfo used for parsing, you can switch to a European culture.

    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);