3

This is kind of related to my previous question, but not really. I have an input of which I don't know the culture. So it can both use ',' and '.' as separator for the fraction. The number will never be >5 though, so we can be rather sure if there's a separator, it will be for the fraction.

I was looking at the TryParse method. It accepts a NumberStyles argument. But I don't see anything about fraction separator or the like..

Am I missing something again or is there a better way to reach my goal?

Community
  • 1
  • 1
Boris Callens
  • 90,659
  • 85
  • 207
  • 305

1 Answers1

9

Try this:

float.TryParse(myString.Replace(',', '.'), out myfloat);

EDIT: as Jon mentioned, the following way is recommended:

float.TryParse(myString.Replace(',', '.'), 
               System.Globalization.NumberStyles.Float, 
               System.Globalization.NumberFormatInfo.InvariantInfo, 
               out myFloat);
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789