Friday, April 3, 2009

Test for Multiple Cultures!


A Silverlight project I'm working on fell victim to "Ugly American Programming" this week – we neglected to thoroughly test the application in non-English languages. This is particularly important for Rich Internet Applications that run on each user's client machine. Each user can have a different culture set in the Regional and Language Options, which looks like this in Vista:



The problem we ran into was that European users were getting a FormatException when executing double.Parse(value) where "value" was set to a decimal number such as "0.05". In European cultures, double.Parse() expects a comma in lieu of a decimal. For example, when I change my regional settings from English to French, the format for decimals changes from "X.Y" to "X,Y". 

English: 

French:
 



It's good practice to always specify "CultureInfo.InvariantCulture" as a second argument to double.Parse() to avoid these problems:
double.Parse(value,CultureInfo.InvariantCulture);

I should really know better – I see this number format every day on my drive in to work courtesy of the folks who programmed my Swedish car: