C# 无法将类型“double”隐式转换为“float”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19671667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type 'double' to 'float'
提问by user2767155
I'm doing a simple program for converting temperatures with Kelvin, Celsius and Fahrenheit, but I'm getting this error when doing anything with kelvin:
我正在做一个用开尔文、摄氏度和华氏度转换温度的简单程序,但是在用开尔文做任何事情时我都会收到这个错误:
Cannon implicitly convert type 'double' to 'float'
The line the error occurs:
发生错误的行:
public static float FahrenheitToKelvin(float fahrenheit)
{
return ((fahrenheit - 32) * 5) / 9 + 273.15;
}
The button click:
按钮点击:
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();
}
And the test method I'm trying to make:
我正在尝试制作的测试方法:
[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.");
}
采纳答案by deathismyfriend
Try this.
尝试这个。
public static float FahrenheitToKelvin(float fahrenheit)
{
return ((fahrenheit - 32f) * 5f) / 9f + 273.15f;
}
This works because it changes the compiler from recognizing the 32 5 and so on as doubles. The f after the number tells the compiler it is a float.
这是有效的,因为它改变了编译器将 32 5 等识别为双打。数字后面的 f 告诉编译器它是一个浮点数。
回答by Luis Filipe
The compiler is promoting the resulting expression to a double. I think it assumes that those numbers which includes a decimal part are double.
编译器将结果表达式提升为双精度。我认为它假设那些包含小数部分的数字是双倍的。
Nevertheless you can explicitly convert the resulting value to a float;
不过,您可以将结果值显式转换为浮点数;
or you can try to explicitly convert all your constants that have a decimal separator to a float.
或者您可以尝试将所有具有小数分隔符的常量显式转换为浮点数。
EDIT
编辑
As a side note, are you perfectly comfortable with using a floats (or doubles) instead of decimals? I don't see your code, eventually when displaying data to the user interfaceby invoking the ToString() method, rounding the value to a certain number of precision digits (although your unit test does it).
作为旁注,您对使用浮点数(或双精度数)而不是小数点是否非常满意?我没有看到您的代码,最终在通过调用ToString() 方法将数据显示到用户界面时,将值四舍五入到一定数量的精度数字(尽管您的单元测试做到了)。
You might get some "lost" precision digits at the end of you floatand they'll get displayed to the UI by using ToString().
您可能会在浮点结束时得到一些“丢失”的精度数字,它们将通过使用 ToString() 显示到 UI 中。
I'd strongly suggest either:
我强烈建议:
- Use round to a certain decimal precision
- Change the floats to decimals
- 使用舍入到某个小数精度
- 将浮点数更改为小数