How to make the division of 2 ints produce a float instead of another int?

后端 未结 9 1120
天命终不由人
天命终不由人 2020-11-22 08:24

In another Bruce Eckels exercise in calculating velocity, v = s / t where s and t are integers. How do I make it so the division cranks out a float?

         


        
9条回答
  •  自闭症患者
    2020-11-22 08:56

    Cast one of the integers to a float to force the operation to be done with floating point math. Otherwise integer math is always preferred. So:

    v = (float)s / t;
    

提交回复
热议问题