Question

Hello I have a mysterious problem.

First I will show you the code that works. I used a new project to test my Problem, but here it works:

Public str As String
Public dbl As Double

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    str = 97.9
    dbl = CDbl(str)
    MsgBox(dbl)

End Sub

Output Msgbox ="97,9"

Now I show you the code which wont work. This is a code snippet from my Main Project. xanswer...ElementAt(index).@similarity.ToString Is the response from the webservice I use and @similarity gives back a Strings with double or Integer values.

 Dim str As String = xanswer.<possibleduplicates>.<address>.ElementAt(index).@similarity.ToString
                Dim dbl As Double = CDbl(str)
                simlist.Add(dbl)

when I hover over str= "97.9" ok, but when I hover over dbl it gives me "979.0" why is that? In the second variable it also uses a "." instead of a "," any idea?

Was it helpful?

Solution

Your string number 97.9 is being converted to double, but in your current computer culture the points act as a thousands separator, and commas as decimals separator, and looks like you want the point to act as a decimal separator. Is a culture problem.

Try this:

str = "97.9"
dbl = Double.Parse(str, Globalization.CultureInfo.InvariantCulture)

Take a look at the MSDN Documentation.

By the way, you should use Option Strict On to prevent implicit conversions of types in your code.

OTHER TIPS

Hello I got it working with this little method

Public Function toDouble(sim As String) As Double
    Dim dbl As Double = CDbl(sim.Replace(".", ","))
    Return dbl

End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top