Math.Pow gives “Cannot implicitly convert type 'double' to 'float' ” error

前端 未结 4 793
暖寄归人
暖寄归人 2021-01-21 12:23

In this program I am trying to create a simple calculator. However, I can\'t seem to find a way to overcome the aforementioned error when reaching the Math.Pow line

4条回答
  •  后悔当初
    2021-01-21 13:13

    You can try explicitly casting the results of Math.Pow to float as such:

    power = (float) Math.Pow(x, 2);
    

    You can also use float's TryParse method to try and parse the result of Math.Pow:

    float.TryParse(Math.Pow(x, 2).ToString(), out power);
    

    Also, you might want to change the format string parameter numbers on the last two Console.WriteLine method calls. They should read like this:

    Console.WriteLine(" {0} squared results in {1}",x, power);
    Console.WriteLine(" Square root of {0} is: {1}", x, sqrt);
    

提交回复
热议问题