You have to use the method that allows you to indicate formats. See TryParse()
.
Any other solution is gambiarra. Even this needs to be well thought out. Imagine if the user types both the point and the comma. What if he put other things on? You can use the culture that more if appropriate what you need.
If this is not enough, and it's a lot better to check if it has an amount or a comma, then you have to create an algorithm that completely checks how it is written and tells you what is wrong in detail, which will be a lot of work. Need to see if it pays off. The form I presented is not perfect, but does not accept wrong formatted data. Any form you accept wrong can not be used. Obviously if the person enters the wrong number in the right format, he has nothing to do.
Any conversion of data entered by a user should be checked if it was successful. In these cases you can never use Parse()
pure because typing is wrong will break the application. This method can only be used when there is certainty of the format of the data to be converted. This is a common mistake that most programmers make.
If you are converting to decimal
, and it is usually the most appropriate, save a variable decimal
and not double
.