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

标签 c# temperature unit-conversion

我从一本 c sharp 书开始就有这个问题 这用于华氏度和摄氏度之间的转换。

    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!?";
    }

当我想从华氏温度得到摄氏温度时,它一直给我 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!?";

也许我对 Order of Ops 有什么误解,但我认为这是真的? 感谢您的帮助。

最佳答案

尝试

5.0F/9.0F

否则您将使用整数运算,其中 5/9 零。

关于c# - 华氏度和摄氏度之间的转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538073/

相关文章:

r - 将以英寸为单位的高度字符向量转换为厘米?

perl - Perl 中的对称和传递自定义单位转换

c# - 使用内部类中存在的值更新进度条

c# - .NET 5.0 打开选择文件夹对话框

python - 获取 Windows 10 PC 的传感器值(例如 GPU 和 CPU 的温度)和风扇速度

graph - Autoit 获取 CPU 温度并将其放入图表中

math - Javascript:计量单位转换库?

c# - 将pdf文件转换为excel表格

c# - 将 silverlight 文件 (xaml) 移动到不同的项目,现在代码隐藏无法识别控件

matplotlib - 将 xarray 每日数据分组为每月平均值