0

I'm trying to make a BMI Calculator (using meters and kilograms) in C#, but, when i type in 1.92m and 80Kg (or any other values), it gives me some weird results, like 0,0021701388888888888888888889.

namespace BMI
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            decimal height = Convert.ToDecimal(Console.ReadLine()); //1.92m
            decimal weight = Convert.ToDecimal(Console.ReadLine()); //80Kg
            decimal BMI = weight / (height*height);
            Console.WriteLine(BMI);// Result: 0,0021701388888888888888888889
        }
    }
}

Isn't it supposed to give me the value of 21.7013888889 without those zeros ? What am i doing wrong ? I am a complete beginner and i have no idea how to fix this.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 5
    Check the content of your height variable (debug the code). Presumably it contains the value 192 because the decimal separator is different in your culture (or better the culture your OS uses). @MakePeaceGreatAgain you shouldn't have removed your comment it was presumably spot on. – Ralf Mar 31 '23 at 14:28
  • @Alex Just pass the actual value like "1.92" . – kaarthick raman Mar 31 '23 at 14:37
  • Looks like you input `192` instead of `1.92` for the height. 80 / (192*192) = 0.00217013888888889 – Mathias R. Jessen Mar 31 '23 at 14:39
  • No, he should input `1,92` and use the same decimal separator he gets in the result. – Ralf Mar 31 '23 at 14:41
  • If you use `System.Globalization` then call `Convert.ToDecimal("1.92", CultureInfo.InvariantCulture)` do you get 1.92 and expected results? – General Grievance Mar 31 '23 at 14:43
  • If you type in `1.92m` then you would get an error - what are you actually typing in for each one? – NetMage Mar 31 '23 at 20:02

2 Answers2

0

Try to do it implicit like this. or Check what you're entering from console...

        string Height_Console = Console.ReadLine();
        decimal height = Convert.ToDecimal(Height_Console); //1.92m
        string Weight_Console = Console.ReadLine();
        decimal weight = Convert.ToDecimal(Weight_Console); //80Kg
        decimal BMI = weight / (height * height);
        Console.WriteLine(BMI);
        Console.ReadKey();

Check this link to get more information about data types DATA TYPES

  • This will most likely result in the same output. The issue is almost certainly culture-related, as hinted by the result in the comment. – General Grievance Mar 31 '23 at 14:53
0

It seems like the problem is in the decimal point. It may not recognize it correctly because some countries use "," and some use "." as the decimal. You can try inputting the height like this:

decimal height = Convert.ToDecimal(Console.ReadLine()) / 100; //192cm

If you don't like this solution, try inputting it with "," instead of "."

quatnu
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '23 at 13:04