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();
}
}
Tim BryantPosted Mar 14, 2014, 3:35 PM
VulpesPosted Mar 14, 2014, 3:19 PM
So it's correct that 5 / 9 == 0.
To force it to use floating point arithmetic instead, use float constants instead of integers i.e.
5.0f / 9.0f == 0.555 recurring