Tim Bryant

Tim Bryant

  • NA
  • 3
  • 1.2k

Math is rounding but I don't want it to.

Mar 14 2014 2:17 PM
I'm doing an exercise to convert between celcius and farenheit. The celcius to farenheit works great but the rounding for the opposite is causing problems.
It appears that saying:
floatVariableName = 5 / 9;
is rounding to 0. Shouldn't a float NOT do that? I've also tried type decimal. Below is my full code... sorry don't laugh... only 1 week into learning this. Please help! I've highlighted the line I think is giving me trouble. Thanks!

static
void Main(string[] args)
{

string convChoice = "";

string endProgram = "Y";

string userTemp = "";

int parsedTemp = 0;

float convertedTemp = 0;

int roundedTemp = 0;

while ((endProgram == "y") || (endProgram == "Y"))

{

Console.WriteLine("If you would like to convert to Celcius please enter C. To convert to Farenheit please enter F.");

convChoice = Console.ReadLine(); //sets their choice of conversion type

if ((convChoice == "c") || (convChoice == "C"))

{

Console.WriteLine("Please enter your original Farenheit value:");

userTemp = Console.ReadLine(); //gets the user value

parsedTemp = int.Parse(userTemp); //converts uservalue to an int for calculation

convertedTemp = (5 / 9) * (parsedTemp - 32);

//roundedTemp = Convert.ToInt32(convertedTemp);

Console.WriteLine("The Celcius equivalent of Farenheit {0} is {1}.", userTemp, convertedTemp);

}

else if ((convChoice == "f") || (convChoice == "F"))

{

Console.WriteLine("Please enter your original Celcius value:");

userTemp = Console.ReadLine(); //gets the user value

parsedTemp = int.Parse(userTemp); //converts uservalue to an int for calculation

convertedTemp = (9 / 5) * (parsedTemp + 32);

roundedTemp = Convert.ToInt32(convertedTemp);

Console.WriteLine("The Farenheit equivalent of Celcius {0} is {1}.", userTemp, roundedTemp);

}

else

{

Console.WriteLine("Invalid Choice");

}

Console.WriteLine("Would you like to try again? Y/N");

endProgram = Console.ReadLine();

}

}


Answers (2)