How to convert latitude/longitude to Double in C#

Asked

Viewed 634 times

3

I have a problem because I have a variable of the type string and I need to convert to the guy double without losing the "dot". Example: string latitude = "-8.709006" when converting is equal to: -8.709006 But so far I’ve 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 I don’t get it:

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 tried too:

string latitude = "-8.709006";
    double lat = Double.Parse(latitude, System.Globalization.CultureInfo.InvariantCulture);
    //E o resultado foi: -8,709006 e nao -8.709006
  • I have also tried using Cultureinfo for "en-BR" and nothing: System.Globalization.Cultureinfo cult = new System.Globalization.Cultureinfo("en");

  • 2

    It worked for me https://dotnetfiddle.net/NMFJe4 I think your problem is not in the conversion but in the presentation.

  • I agree with @bigown, the problem seems to be in the presentation and not in the conversion itself.

  • @bigown aceibei de da da uma Ctrl+C in your code and the result remains the same here for min: -8,709006 really no longer what is happening!

  • @Joaquimcaetanoteixeira Already checked the time zone and language settings in the Control Panel?

  • @Joaquimcaetanoteixeira The type double stored in memory, has nothing to do with string representation using , or .. When you will check the value after doing the conversion, what is shown to you is the representation in string form, using the language configuration of your operating system.

  • @Joaquimcaetanoteixeira Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need something to be improved?

Show 2 more comments

1 answer

3

The conversion seems to be working ok. If you show it in a specific format you need to specify this format:

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • in this case it is really working, but it is the following for me to get this result of the last line it is already converted again to type "string"

  • There are several ways to do this one of them is like this, you take the string formatted the way you want.

  • is that I need it formatted but double type.

  • lat is double type. A numeric type has no formatting. It is a number. Only stringss have formatting. When you want to do calculations you do with the number, no matter the formatting. When you want to present the number there you present as string. It is only possible to present as string. Even if you are not seeing it, a conversion is always done to present it. There is no other way to do it in any language. What changes from one language to another is just the detail.

  • But it’s that in my case I don’t do any calculations with it, I’m using it for latitude and longitude with the 'Gmap API'. NET' and if I pass it without the point it goes to a totally different location and the method of Gmap . NET receive as parameter the double type.

  • There you are talking about a specific API. I don’t know how it passes the data. But either it passes as type double and no matter the format, because the type has no format or goes through string and then I gave you the way to format as you want. Other than that, you need to ask a question with your real problem. What you asked, I answered.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.