Pallavi kakade
Pallavi kakade

Reputation: 1

Convert.ToSingle(value, new CultureInfo("fr")) method gives 'Exception' for french culture but its working for German culutre

I am using Convert.tosingle() method to convert value to float. This method working fine for 'German' and 'Spanish' culture but its giving me Exception of 'Input string was not in correct format' for Polish and French Culture. How to solve that. for French and polish I want to display values as "0,85"

Below is the code

string value = "0.85";
 float floatValue = Convert.ToSingle(value, new CultureInfo("de"));
 //Working fine
 float floatValue1 = Convert.ToSingle(value, new CultureInfo("es"));
 //Working fine


 float floatValue2 = Convert.ToSingle(value, new CultureInfo("fr"));  //      Giving Exception for French culture
float floatValue3 = Convert.ToSingle(value, new CultureInfo("pl"));
//Exception for Polish culture

Thanks, Pallavi

Upvotes: 0

Views: 253

Answers (2)

Fendy
Fendy

Reputation: 4643

That's because french does not has thousand separator while german has dot as thousand separator.

    float value = 1234.56f;
    Console.WriteLine(value.ToString("#,##0.##", CultureInfo.GetCultureInfo("fr")));
    Console.WriteLine(value.ToString("#,##0.##", CultureInfo.GetCultureInfo("de")));

result:

1 234,56
1.234,56

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

That's because in Polish (and most likely French) culture , (a comma) is used as decimal separator, not a dot.

So your value have to be 0,85 to make it work for those cultures.

Upvotes: 2

Related Questions