Converting string to double in different locale
Posted on 2005-04-25
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.