C# .NET - Double.tryParse is behaving differently with different culture

Asked By rajeev chandra on 18-Jul-13 02:06 PM

class Program
  static void Main(string[] args)
    CultureInfo Greek = CultureInfo.GetCultureInfo("el-GR");
    CultureInfo US = CultureInfo.GetCultureInfo("en-US");
    string storedValue = "649.54";
    double amount = 0;
    double.TryParse(storedValue, NumberStyles.Any, Greek, out amount);
    Console.WriteLine("Current Culture = {0} , Amount = {1}", Greek, amount); --Output 64954     double.TryParse(storedValue, NumberStyles.Any, US, out amount);
    Console.WriteLine("Current Culture = {0} , Amount = {1}", US, amount);
  --Output 649.54

When the culture is in "en-us" working perfectly but when the culture is in "el-GR" it is showing the output in wrong format. Please help me to parse so that it can work for any culture in c# for numbers.
Robbe Morris replied to rajeev chandra on 18-Jul-13 02:17 PM
You have to know the culture of the value "before" you do a parse on it.  You are expecting the .TryParse to understand that you want it to convert a string into Greek.  It has no idea what to convert it "from".  US or Greek?  It has no concept of whether the decimal or commas are in actual US Dollars, Euros, or something else.  So, it won't know what to do.

As a general rule with multi-currency apps, your team will need to decide on a culture that the data will be stored in and then alter the "display" when shown to the user.  In other words, no matter what you convert the value to in the app, it must always be converted back to x cultureinfo as it goes into your database.