Search code examples
c#asp.netglobalization

Globalization .net convert string to double


İ have string like this and i want to convert it to double.

string x = "65.50";
double y = Convert.ToDouble(x);

But the result is 6550.0

i want it to be 65.50.

I am using ASP.NET and C#. I think it is a problem about globalization.

This is my for question sorry about that (:


Solution

  • Yes, it's your current culture that converts it this way. You can use CultureInfo.InvariantCulture to skip using your culture.

    double d = double.Parse("65.50", CultureInfo.InvariantCulture);
    

    i want it to be 65.50.

    If you want to convert it back to string:

    string str = d.ToString("N2", CultureInfo.InvariantCulture);  
    

    I assume this is a currency since you keep the decimal places. Then you should use decimal instead:

    decimal dec = decimal.Parse("65.50", CultureInfo.InvariantCulture); // 65.5
    

    Now you can use decimal.ToString and it automagically restores the decimal places:

    string str = dec.ToString(CultureInfo.InvariantCulture); // "65.50"