c# - 从华氏度转换为摄氏度

标签 c#

我正在尝试将华氏温度转换为摄氏温度。
做以下我总是得到零:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Celcius_Farenheit_Converter
{
class Program
{

    static double Celcius(double f)
    {
        double c = 5/9*(f - 32);

        return c;
    }
    static void Main(string[] args)
    {
        string text = "enter a farenheit tempature";
        double c = Celcius(GetTempature(text));
        Console.WriteLine("the tempature in Celicus is {0}", c);

        Console.ReadKey(true);

    }

    static double GetTempature(string text)
    {
        Console.WriteLine(text);
        bool IsItTemp = false;
        double x = 0;

        do
        {
            IsItTemp = double.TryParse(Console.ReadLine(), out x);
        } while (!IsItTemp);

        return x;

    }
}
}

你能帮我解决一下吗?

最佳答案

5/9 执行整数除法——也就是说,它总是丢弃小数部分——所以它总是返回 0。

5.0/9.0 执行浮点除法,将返回预期的 0.55555...

试试这个:

static double Celcius(double f)
{
    double c = 5.0/9.0 * (f - 32);

    return c;
}

延伸阅读

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

相关文章:

c# - 无法使用 Datetime.ParseExact 转换日期时间字符串

c# - 使用 XmlDocument 从子节点读取属性字符串

c# - 不允许对 IsolatedStorageFileStream 进行操作。错误

c# - 如何使用泛型?

c# - 如何反序列化当前类

c# - 如何正确循环并打印 Int、Long、Float 或 BigInteger 的位?

c# - 如何在 updatepanel 异步回发后在客户端延迟更新

c# - 创建登录屏幕,WPF,如何存储用户名和密码

c# - 如何在 LinqToSQL 中搜索表?

c# - 无法使用 GraphServiceClient 创建具有应用程序身份的在线 session