Trouble converting a string to double

zack

Well-known member
Joined
Jun 9, 2005
Messages
65
Programming Experience
Beginner
Hi, I'm trying to convert a string to double but this error always occurs: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

While myReader.Peek() <> -1
Label1.Text = myReader.ReadLine()
Dim test As String = Label1.Text
Dim lat1 As String
Dim lon1 As String
lat1 = test.Substring(0, 11)
lon1 = test.Substring(15, 8)
Dim dlat1 As Double
Dim dlon1 As Double
dlat1 = System.Convert.ToDouble(lat1)
dlon1 = System.Convert.ToDouble(lon1) <- code in error It is the same even if i use CDbl
Myevent.AutoRead(dlat1, dlat1)
End While

The attachment below is what i am trying to convert:
103.008764, n, 1.2088983, e
 
Hi Zack,

You may want to try doing

lat1 = test.Substring(0, 10)

rather than doing,

lat1 = test.Substring(0, 11) (Appends a "Comma" literal for conversion which is not recommended)

I Guess this has to make that work.

Regards,
Vishal Adsool

 
A true Windows CSV file should be like this:
"value1","value2","value3"

In your case, you could still Split on the comma and then call Trim on each string before using it to remove the spaces, e.g.
VB.NET:
Dim myStrings As String() = myReader.ReadLine().Split(","c)
 Dim firstNumber as Double = CDbl(myStrings(0).Trim())
 Dim secondNumber as Double = CDbl(myStrings(2).Trim())
 
Thanks for the help guys.
There were actually some miscalculations n misplacement in my codes.
 
Back
Top