无法将类型'double'隐式转换为'float'

时间:2021-08-20 19:19:17

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.");
}

2 个解决方案

#1


8  

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告诉编译器它是一个浮点数。

#2


0  

The compiler is promoting the resulting expression to a double. I think it assumes that those numbers which includes a decimal part are double.

编译器将结果表达式提升为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.

或者您可以尝试将具有小数分隔符的所有常量显式转换为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 interface by 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 float and they'll get displayed to the UI by using ToString().

您可能会在浮动结束时获得一些“丢失”的精确数字,并且它们将通过使用ToString()显示到UI。

I'd strongly suggest either:

我强烈建议:

  1. Use round to a certain decimal precision
  2. 使用舍入到某个小数精度

  3. Change the floats to decimals
  4. 将浮点数更改为小数

#1


8  

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告诉编译器它是一个浮点数。

#2


0  

The compiler is promoting the resulting expression to a double. I think it assumes that those numbers which includes a decimal part are double.

编译器将结果表达式提升为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.

或者您可以尝试将具有小数分隔符的所有常量显式转换为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 interface by 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 float and they'll get displayed to the UI by using ToString().

您可能会在浮动结束时获得一些“丢失”的精确数字,并且它们将通过使用ToString()显示到UI。

I'd strongly suggest either:

我强烈建议:

  1. Use round to a certain decimal precision
  2. 使用舍入到某个小数精度

  3. Change the floats to decimals
  4. 将浮点数更改为小数