我正在尝试将华氏温度转换为摄氏温度。
做以下我总是得到零:
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/