How to convert latitude / longitude to Double in C #

2

I have a problem, as it has a variable of type string and I need to convert to type double without losing the "dot". Example: string latitude = "-8.709006" when converting is equal to: -8.709006 But until then I have done several different tests and nothing.

string latitude = "-8.709006";
double lat = Double.Parse(latitude);
//Mas ele esta me trazendo: -8709006 e nao -8.709006

I've done it another way too but it does not get any results:

string latitude = "-8.709006";
System.Globalization.CultureInfo cult = new System.Globalization.CultureInfo("en-US");
double lat = double.Parse(latitude, cult);
//Desta vez o resultado foi: -8,709006 e nao -8.709006

As I also tried this:

string latitude = "-8.709006";
    double lat = Double.Parse(latitude, System.Globalization.CultureInfo.InvariantCulture);
    //E o resultado foi: -8,709006 e nao -8.709006
    
asked by anonymous 25.02.2015 / 14:35

1 answer

1

The conversion looks like it's working okay. If you display in a specific format you need to specify this format:

using System;

public class Program {
    public static void Main() {
        string latitude = "-8.709006";
        double lat = Double.Parse(latitude, System.Globalization.CultureInfo.InvariantCulture);
        Console.WriteLine(lat.ToString(new System.Globalization.CultureInfo("en-US", true)));
    }
}

See working on dotNetFiddle .

    
25.02.2015 / 15:13