← Previous

Today I had a very bizarre bug in which latitudes and longitudes that needed to be parsed from string to double was failing. The code was simple:

double checklat;
bool isValidLat = Double.TryParse(currentLocation.latitude, out checklat);
if(isValidLat) {
     //do something
}
isValidNav was return false for completely valid numbers for no apparent reason. It was a very strange bug. It turns out that in france, decimals are written as commas. So in french sites 45.232 would be written as 45,232. The try-parse was detecting that the browser was a french local and tried to parse it as such. In the end, taking this into account, the following code fixed the problem:
bool isValidLat = Double.TryParse(currentLocation.latitude, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out checklat);
This ensured that even in french sites, that the double would try-parse according to US standards. Kindle E-Reader Comparison Site
Next →