Division result is not correct

后端 未结 2 791
无人及你
无人及你 2020-12-22 00:59
CGFloat result=100/126;
NSLog(@\"%0.5f\",result);

the output of above code is 0.00000 but when i check it on calculator then the value is 0.79365.

2条回答
  •  不知归路
    2020-12-22 01:42

    The problem is as both operands of division are integers, integer division is performed and its result is 0. To get proper value you need to make sure that at least one of your operands is floating point number:

    CGFloat result=100.0f/126;
    

提交回复
热议问题