[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

Converting string to double in different locale

In my app I read in some latitudes and longitudes from an xml file. They are in decimal format so they take values such as 43.4579283 or 138.3489543.
Since they are to be used in an equation I need them in double format. This is easy I can open the xml, read the values and convert them to the double type.

The problem I have is that in other countries where the decimal placeholder is not a decimal point (.) but a comma (,) vb cannot convert the string to a double. Ie I try to to read the numbers 138.3489543 but it seems that vb.net is expecting them in the format 138,3489543. The problem can be fixed by the user changing their windows regional settings to use a decimal point rather than a comma. However, Id rather that my program work in these countries without system changes being required. My current code reads:

Dim Lat as Double
Lat = Conversion.Val(x_node.GetElementsByTagName("FrontRightLat").ItemOf(0).Attributes(0).Value)

Where a comma is in use users get the error:
System.InvalidCastException: Cast from string "37.0342853233869" to type 'Double' is not valid. ---> System.FormatException: Input string was not in a correct format.

This code works flawlessly otherwise.

Any suggestions?
0
vulpine
Asked:
vulpine
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
Use the parse method of the double object, it takes into account the currect culture info

like

'if the culture uses a ,
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("nl-NL")
        Dim x As String = "43,4579283"
        Dim y As Double = Double.Parse(x)
        MsgBox(y)

'if the culture uses a .
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-US")
        Dim x As String = "43.4579283"
        Dim y As Double = Double.Parse(x)
        MsgBox(y)
0
 
RonaldBiemansCommented:
sorry, maybe it is better to provide the culture info to the parse method like

        System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("nl-NL")
        Dim x As String = "43,4579283"
        Dim y As Double = Double.Parse(x, System.Threading.Thread.CurrentThread.CurrentUICulture)
        MsgBox(y)

        System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-US")
        Dim x2 As String = "43.4579283"
        Dim y2 As Double = Double.Parse(x2, System.Threading.Thread.CurrentThread.CurrentUICulture)
        MsgBox(y2)
0
 
vulpineAuthor Commented:
That looks good but Id have to specify the location. Is there a way to detect the location automatically?
0
 
RonaldBiemansCommented:
The above code does that
you just have to leave out the first line,

this one can be left out (I just used it to show that it work when there are diiferent cultures

System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-US")

This detect the current location "System.Threading.Thread.CurrentThread.CurrentUICulture"

so you would only have to do this

Dim Lat as Double
Lat = double.parse(x_node.GetElementsByTagName("FrontRightLat").ItemOf(0).Attributes(0).Value,System.Threading.Thread.CurrentThread.CurrentUICulture)

this does have to return a string though

x_node.GetElementsByTagName("FrontRightLat").ItemOf(0).Attributes(0).Value
0
 
vulpineAuthor Commented:
Thanks heaps!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now