华氏度和摄氏度之间的转换问题

use*_*825 2 c# temperature unit-conversion

我从一开始就有这个问题,这是华氏和摄氏之间的转换.

    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!?";
    }
Run Code Online (Sandbox Code Playgroud)

当我想从华氏温度得到摄氏温度时它会一直给我0,即使这个公式似乎对我来说也是如此.这有什么不对?
因为我认为问题出在这里:

        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!?";
Run Code Online (Sandbox Code Playgroud)

也许我对Ops顺序有问题,但我认为这是真的吗?感谢帮助.

Mar*_*ell 7

尝试

5.0F/9.0F
Run Code Online (Sandbox Code Playgroud)

您正在以其他方式使用整数运算,其中5/9 零.


Liv*_* M. 5

尝试在那里放置另一个演员表以确保确定,如下所示:

cel = ((float)5/9)*(fahr-32);
Run Code Online (Sandbox Code Playgroud)

最有可能 5/9 评估为整数并给出 0。其他选项如下:

cel = (5f/9f)*(fahr-32);
Run Code Online (Sandbox Code Playgroud)