double.Parse(string) problem

M

Mika M

Hi,

Just for fun I'm trying to parse my GPS position string using C# 2005.

When my code is trying to parse latitude string to double value like...

double.Parse(items[2]); // items[2] = "6215.1058"

....I'll just get an error. What's wrong with that?

Immediate Window:

?items
{Dimensions:[15]}
[0]: "GPGGA"
[1]: "065026.000"
[2]: "6215.1058"
[3]: "N"
[4]: "02546.1629"
[5]: "E"
[6]: "1"
[7]: "05"
[8]: "2.7"
[9]: "171.8"
[10]: "M"
[11]: "19.7"
[12]: "M"
[13]: ""
[14]: "0000*5E\r\n"
?items[2]
"6215.1058"
?double.Parse(items[2])
'double.Parse(items[2])' threw an exception of type 'System.FormatException'
base {System.SystemException}: {"Input string was not in a correct
format."}

Now you know at least where I am right now :)
 
G

Göran Andersson

Mika said:
?items[2]
"6215.1058"
?double.Parse(items[2])
'double.Parse(items[2])' threw an exception of type
'System.FormatException'
base {System.SystemException}: {"Input string was not in a correct
format."}

You probably have a culture setting where the period is not the decimal
separator. Specify the invariant culture when you parse the string:

double.Parse(items[2], CultureInfo.InvariantCulture)
 
M

Mika M

Yes Göran you are right, thank you!

Hälsningar från Finland :)


Göran Andersson said:
Mika said:
?items[2]
"6215.1058"
?double.Parse(items[2])
'double.Parse(items[2])' threw an exception of type
'System.FormatException'
base {System.SystemException}: {"Input string was not in a correct
format."}

You probably have a culture setting where the period is not the decimal
separator. Specify the invariant culture when you parse the string:

double.Parse(items[2], CultureInfo.InvariantCulture)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top