VS:2005, framework: 2.0
I have a dropdown with French and English languages. As User selects a language, We set CultureInfo as fr-FR or en-US respectively.
When retrieving float values from DB we assign value as
Convert.ToDecimal(table.Rows[0]["diametre"].ToString(), System.Globalization.CultureInfo.InvariantCulture)
In french, 1.3 is displayed as 13 or sometime gives "input string was not in a correct format." error. I tried follwing code:
System.Globalization.NumberFormatInfo numberFormatInfo = new System.Globalization.NumberFormatInfo();
if (System.Globalization.CultureInfo.CurrentCulture.Name == "fr-FR")
numberFormatInfo.NumberDecimalSeparator = ",";
else
numberFormatInfo.NumberDecimalSeparator = ".";
Convert.ToDecimal(table.Rows[0]["densite"].ToString(), numberFormatInfo)
But gives Input String error as above. When "en-US" culture, it works perfectly fine!
What else should I try? This is giving quite a pain now. Please help me. TIA
Update: Thanks all, for responses! I had help from a colleague while fixing bug. We did following change:
decimal diametre = Convert.ToDecimal(table.Rows[0]["diametre"].ToString().Replace(",", "."), new System.Globalization.CultureInfo("en-US"));
Thanks Tim for useful info like "ToString will use the current culture's decimal separator"