Search code examples
c#globalization

Double.Parse on a german system result in comma instead of decimal point


I have the following:

 private static string someValue = "0.1";

I need to convert this into double in order to do some calculation. On a German system I get the conversion as 0,1 rather than 0.1, I need it as 0.1. I tried the following:

double d = Convert.ToDouble(s_valueDeadband10Percent, CultureInfo.InvariantCulture.NumberFormat); // still getting 0,1

AND

double d = Convert.ToDouble(s_valueDeadband10Percent, CultureInfo.InvariantCulture); // still getting 0,1

Any ideas ?


Solution

  • On a German system I get the conversion as 0,1 rather than 0.1, I need it as 0.1.

    No, you don't get either of those. You get a double value which is approximately a tenth. A double value isn't a textual "thing" at all. It can be converted to text, but that's a different operation. You need to find where that operation is, and change that to use CultureInfo.InvariantCulture, if you really want a text value. But if you can get away without the conversion back to a string at all, that would be even better.

    Of course, if possible, you should change your static variable to be a double to start with:

    private static double someValue = 0.1;
    

    That way you can avoid parsing, too.