2

I have a string value, containing longitude or latitude value.

However, when I try to convert this to a double, I end up with a number where the dot is removed and placed at the end.

This is not what I want. What am I missing?

This is the value I get: 200,5,1.4928184,0.1609203 and this is the method I use to get the value:

 var responseBytes = ms.ToArray();
                var encoding = new System.Text.ASCIIEncoding();
                var coords = encoding.GetString(responseBytes);
                var parts = coords.Split(new char[] { ',' });


//this piece of code returns me wrong values! When I debug this piece parts[2] is exactly 1.4928184
                return new Coordinaat(Double.Parse(parts[2]),
                    Double.Parse(parts[3]));

See code example below for constructor:

 public Coordinaat(double lat, double lon)
        {
            this.Latitude = lat;
            this.Longitude = lon;
        }

Output: lat 114928184 lon 01609203 --> where did my dots go to??

Rob
  • 3,556
  • 2
  • 34
  • 53

1 Answers1

7

You are probably under a culture (e.g German) which is causing this effect. Use CultureInfo.InvariantCultre while parsing.

Double.Parse("123.22",CultureInfo.InvariantCulture)
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Dutch system probably same as German culture. This fixed it. Thanks. – Rob Feb 20 '13 at 12:34
  • Or under Dutch culture (or almost any continental European culture). – Jeppe Stig Nielsen Feb 20 '13 at 12:34
  • 1
    @dtb, had the similar experience with German culture, didn't know that its true for most of the European culture – Habib Feb 20 '13 at 12:37
  • To elaborate, under a Dutch culture, the separator `"."` is considered some arbitrary "grouping" of the digits (thousands separator, `NumberGroupSeparator`), and `","` is the character separating the integer part from the decimals, so `42.012.345,67` is a "good" Dutch number. And `1.4928184` will be the same as `14.928.184` (the grouping separators are more or less ignored, only commas matter). – Jeppe Stig Nielsen Feb 20 '13 at 12:42
  • I get the same problem even when using invariantCulture – phil123456 Mar 08 '21 at 09:01