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

Asked

Viewed 1,451 times

5

I am developing an exercise whose result is different in scenarios that the user uses point or comma to put his decimal number, in this case I put as variable of type double

Console.WriteLine("Caculadora de Imc\n\n\n");
Console.WriteLine("Digite o seu peso atual \n");
int peso = int.Parse(Console.ReadLine());
Console.WriteLine("Digite agora a sua altura \n");
double altura = decimal.Parse(Console.ReadLine());
double imc = peso / (altura * altura);
Console.WriteLine("Seu imc corresponde a {0}", imc);
Console.ReadKey();

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

  • You want to ignore what the user type, and always calculate as comma?

  • 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

  • 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 =]

2 answers

5


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.

4

To check if he is using a point or comma, you can use the 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 comma to point and vice versa, you can use the Replace():

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.