Cannot implicitly convert type 'double' to 'float'

后端 未结 2 1776
旧时难觅i
旧时难觅i 2020-12-19 04:10

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:

Can         


        
2条回答
  •  猫巷女王i
    2020-12-19 04:18

    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.

提交回复
热议问题