This is basically a CultureInfo problem. Formally, in my country, the decimal separator is a comma (,), and the thousands separator is a period (.). In practice, however, this is only used by accountants and diligent people. Typically, people never use the thousands separator, and they use both a comma and a period as a decimal separator. I saw that this is a problem even in some Excel spreadsheets that I received from other people, and Excel did not recognize the dot as a decimal separator, leaving a field formatted as a string, not a number.
My "solution" so far has been to simply replace all the commas in the user input with dots, and then parse the double with InvariantCulture, for example:
string userInput; ... userInput = userInput.Replace(',', '.'); double result; double.TryParse(userInput, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
This will obviously fail when someone really enters the thousands separator, and it seems to me more like a hack than a real solution. So, in addition to creating your own parser for pairs, are there any cleaner ways to solve this problem?
source share