You have to use the method that allows you to specify formats. See TryParse()
.
Any other solution is gambiarra. Even this one needs to be well thought out. Imagine if the user type both the dot, and the comma. What if he puts other things? You can use the culture which more suited to what it needs.
If this is not enough, and it is much better to check if you have a heap or a comma, then you will have to create an algorithm that completely checks how it is written and say in detail what is wrong, which will be a lot of work. Need to see if it pays off. The shape I presented is not perfect, but does not accept wrong formatted data. Any form that accepts wrong data can not be used. Obviously if the person enters a wrong number in the right format has nothing to do.
Any data conversion entered by a user must be checked if it was successful. In these cases you can never use the Parse()
pure because if typing is wrong it will break the application. This method can only be used when there is certainty of the data format to be converted. This is a common mistake that most programmers make.
If you’re converting to decimal
, and is usually the most suitable, save a variable decimal
and not double
.
You want to ignore what the user type, and always calculate as comma?
– Rovann Linhalis
I actually want to identify that he used stitch and warn him to use comma, but always calculate with comma regardless if he use stitch seems a good
– João Ávila
In a graphical interface you can better handle this, but the correct thing is to let the user pass with a semicolon and/or a dot, it is okay that in its scope only enter the point if the weight of the person was informed in grams (another problem you do not specify the unit the user needs to report) but it is not the case. I suggest you do as Maniero said, using the Tryparse, Just a few days ago I answered a conversion question you can see here: https://answall.com/a/251920/69359 . I hope it helps =]
– Rovann Linhalis