Ints and Doubles doing division

后端 未结 5 1865
挽巷
挽巷 2021-01-19 21:33

Short version: Why don\'t I have to coerce 60, and int, into a double, so that I can use division with another double if I DO care about the fractional part?

Long ve

5条回答
  •  执念已碎
    2021-01-19 22:01

    You can't divide a double by an integer, so the compiler will cast the integer to a double.

    So, the expressions (double)durationinseconds / 60 and (double)durationinseconds / 60.0 will result in identical code being created.

    I prefer using 60.0 in this case just to make it more visible what the code is actually doing.

提交回复
热议问题