0

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"

4 Answers4

2

Why is diametre a string at all? If it's a decimal in the database you should use:

decimal diametre = table.Rows[0].Field<decimal>("diametre");

That will be more efficient and also prevents localization issues.

Otherwise you can use decimal.Parse with the appropriate culture-info:

var french = new CultureInfo("fr-FR");
decimal diametre = decimal.Parse(table.Rows[0].Field<string>("diametre"), french);

Update i've only just seen that you're using .NET2, then the Field-extension method is not available. You have to cast it to the correct type, i still wouldn't use ToString to convert everything to string since that could modify it(f.e. if it's a decimal, ToString will use the current culture's decimal separator).

decimal diametre = (decimal)table.Rows[0]["diametre"];

or

decimal diametre = decimal.Parse((string)table.Rows[0]["diametre"], french);

If it's always stored with the dot as decimal separator as in en-US for example, you can use:

decimal diametre = decimal.Parse((string)table.Rows[0]["diametre"], CultureInfo.InvariantCulture);

If you then want to diplay it with the correct format use decimal.ToString:

string output = diametre.ToString(french);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • The "appropriate culture-info" mentioned above is always CultureInfo.Invariant for any backend facing string input/output, otherwise you will have data conversion issues. – Eric MSFT Sep 05 '14 at 19:28
  • @Tim, How to I use this, decimal diametre = table.Rows[0].Field("diametre"); Can u provide real code example? – Akash Agrawal Sep 08 '14 at 08:56
  • @AkashAgrawal: that is real code. What are you trying to achieve? Do you want to loop the table? – Tim Schmelter Sep 08 '14 at 08:59
  • @Tim: No. But when I use code as "decimal diametre = table.Rows[0]["diametre"]("diametre");" , it give me error as "Incorrect syntax near ". Sorry for being noob/ignorant! – Akash Agrawal Sep 08 '14 at 09:08
  • @AkashAgrawal: in your first comment you have used the correct syntax and in the last comment incorrect syntax ;) – Tim Schmelter Sep 08 '14 at 09:10
  • @AkashAgrawal: but if you're using .NET 2 as stated in your question you cannot use the [`Field`-method](http://msdn.microsoft.com/en-us/library/bb360891%28v=vs.90%29.aspx) which was added in .NET 3.5. But i have mentioned that also in my answer (**Update**). – Tim Schmelter Sep 08 '14 at 09:22
  • yup, I tried code on 4.0. Thanks. Could you tell me how do I close this question? :) – Akash Agrawal Sep 08 '14 at 09:29
  • @AkashAgrawal: To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Tim Schmelter Sep 08 '14 at 09:32
0

Basiclly, you have 2 cultures in .NET.

You have a culture and you have a UI culture.

The Culture affects how culture-dependent data (dates, currencies, numbers and other information) is being displayed to the user. The UI culture affects the resource file to be used, so first of all, if you just want to change the language, but keeping the date formates and other information like configured on the computer, you should only change the UI culture and not the culture.

However, the answers given above are valid.

  • If the field is a number, store it as a number and not a string.
  • Use Parse with the correct culture.

@Tim I gave you credits for your answer, but I wanted to contribute with this aswell.

Complexity
  • 5,682
  • 6
  • 41
  • 84
0

I know this question is old, but it might help others who have the same issue and none the above solutions has helped them. For me, adding this line of code to the MainPage constructor of my app (I'm using xamarin.Forms), solved the issue.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); 

Hope it helps, it simply sets the culture of the application to english, regardless of what the devices culture/language has been set to, so if you need the app to behave locally, then this might not be a good solution

-1

You can convert your dbValue first like this:

string MyValue = (String)(table.Rows[0]["diametre"] == DBNull.Value ? string.Empty : table.Rows[0]["diametre"].ToString);

then replace possible comma by dot:

if(!string.isNullOrEmpty(MyValue)){
MyValue.Replace(",",".");
}

and then Convert it to decimal:

Convert.ToDecimal(MyValue, System.Globalization.CultureInfo.InvariantCulture)
Anton Selin
  • 3,462
  • 6
  • 19
  • 23
  • The application might fail again when you're changing the culture when using your code since you're replacing characters manually. – Complexity Sep 05 '14 at 14:11
  • Can you provide any example in which application will fail? – Anton Selin Sep 05 '14 at 14:14
  • Sorry, I was wrong, this will not fail, but you're changing the display of your number directly without using the Cultures. The OP clearly states that he's using cultures to switch and because of another culture, the numbers are giving an error. – Complexity Sep 05 '14 at 14:16