Conversion Between Fahrenheit and Celsius ISSUE
i have this problem from a beginning c sharp book this for conversion between Fahrenheit and Celsius.
private void button1_Click(object sender, EventArgs e)
{
float fahr, cel;
if (Celsius.Checked == true)
{
fahr = float.Parse(textBox1.Text);
cel = (5/9)*(fahr-32);
richTextBox1.Text = "The Degree in Celsius is:" + cel.ToString() + Environment.NewLine + "cool isn't it!?";
}
else if (Fahrenheit.Checked == true )
{
cel = float.Parse(textBox1.Text);
fahr = ((9 * cel)/5)+ 32;
richTextBox1.Text = "The degree in Fahrenheit is:" + fahr.ToString() + Environment.NewLine + "cool is it!?";
}
when i want to get Celsius from a Fahrenheit it keeps giving me 0 even though the formula appears to be true to me. what's wrong here?
because i think the problem lies here: if (Celsius.Checked == true)
{
fahr = float.Parse(textBox1.Text);
cel = (5/9)*(fahr-32);
richTextBox1.Text = "The Degree in Celsius is:" + cel.ToString() + En开发者_开发问答vironment.NewLine + "cool isn't it!?";
maybe i have something wrong with Order of Ops but i think it's True? thanks for help.
Try
5.0F/9.0F
You are otherwise using integer arithmetic, where 5/9 is zero.
Try putting another cast there just to be sure, like this:
cel = ((float)5/9)*(fahr-32);
Most probably 5/9 evaluate as ints and gives 0. Other option would be like this:
cel = (5f/9f)*(fahr-32);
精彩评论