Akash Agrawal
Akash Agrawal

Reputation: 51

input string was not in a correct format. c# CultureInfo

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"

Upvotes: 0

Views: 2417

Answers (4)

Reihaneh Khaksaran
Reihaneh Khaksaran

Reputation: 228

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460340

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);

Upvotes: 2

Anton Selin
Anton Selin

Reputation: 3642

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)

Upvotes: -1

Complexity
Complexity

Reputation: 5830

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.

Upvotes: 0

Related Questions