# I need to know when the user uses semicolons in decimal numbers

Posted on

#### Question :

I’m developing an exercise whose result is different in scenarios that the user uses semicolon to place their decimal number, in case I put it as a variable of type `double`

``````Console.WriteLine("Caculadora de Imcnnn");
Console.WriteLine("Digite o seu peso atual n");
Console.WriteLine("Digite agora a sua altura n");
double imc = peso / (altura * altura);
Console.WriteLine("Seu imc corresponde a {0}", imc);
``````

How do I know when the user types a comma or dot in the decimal?

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` .

To check if it is using a semicolon, you can use `Contains()` :

``````string alturaIn = Console.ReadLine();
if (alturaIn.Contains(".")){
Console.WriteLine("Contem ponto.");
}
else if (alturaIn.Contains(",")){
Console.WriteLine("Contem virgula.");
}
double altura = double.Parse(alturaIn);
double imc = peso / (altura * altura);
``````

If you want to change from comma to point and vice versa, you can use `Replace()` :

``````if (alturaIn.Contains(".")){
alturaIn.Replace(".", ","); //Troca por virgula
}
else if (alturaIn.Contains(",")){
alturaIn.Replace(",", "."); //Troca por ponto
}
``````