c# – 无法将类型’double’隐式转换为’float’
发布时间:2020-12-15 04:14:01 所属栏目:百科 来源:网络整理
导读:我正在做一个用开尔文,摄氏和华氏温度转换温度的简单程序,但是在用开尔文做任何事情时我都会遇到这个错误: Cannon implicitly convert type 'double' to 'float' 发生错误的行: public static float FahrenheitToKelvin(float fahrenheit){ return ((fahre
我正在做一个用开尔文,摄氏和华氏温度转换温度的简单程序,但是在用开尔文做任何事情时我都会遇到这个错误:
Cannon implicitly convert type 'double' to 'float' 发生错误的行: public static float FahrenheitToKelvin(float fahrenheit) { return ((fahrenheit - 32) * 5) / 9 + 273.15; } 按钮点击: private void button1_Click(object sender,EventArgs e) { var input = float.Parse(textBox1.Text); float FahrenResult = FahrenheitToCelsius(input); textBox2.Text = FahrenResult.ToString(); float KelvinResult = FahrenheitToKelvin(input); textBox3.Text = KelvinResult.ToString(); } 我试图做的测试方法: [TestMethod] public void fahrenheitToKelvinBoiling() { float fahrenheit = 212F; float expected = 373.15F; // TODO: Initialize to an appropriate value float actual; actual = Form1.FahrenheitToKelvin(fahrenheit); Assert.AreEqual(Math.Round(expected,2),Math.Round(actual,2)); // Assert.Inconclusive("Verify the correctness of this test method."); } 解决方法
试试这个.
public static float FahrenheitToKelvin(float fahrenheit) { return ((fahrenheit - 32f) * 5f) / 9f + 273.15f; } 这是有效的,因为它将编译器从识别32 5等变为双倍.数字后面的f告诉编译器它是一个浮点数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- PostgreSQL中的IF函数和MySQL一样
- 使用序列在表中插入值时,oracle 11.2.0.1.0和oracle 11.2.0
- C语言文件随机访问fseek()和ftell()函数
- c# – .net core websockets获取DbContext
- ruby-on-rails – Rails 3和Devise:重定向到注册页面(可确
- ruby-on-rails – Hirb – “太多的字段……”仅在从irbrc加
- Ajax的核心技术XMLHttpRequest方法
- 用PULL解析XML文件时循环的过程
- Oracle 数据库针对表主键列并发导致行级锁简单演示
- C++ find_first_of(STL find_first_of)查找算法详解