分割结果不正确 [英] Division result is not correct

查看:146
本文介绍了分割结果不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CGFloat result=100/126;
NSLog(@"%0.5f",result);

上述代码的输出为0.00000,但当我在计算器上检查时,该值为0.79365。是什么问题

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

推荐答案

问题在于,除法的两个操作数都是整数,执行整数除法,结果为0。要获得正确的值,您需要确保至少有一个操作数是浮点数:

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;

这篇关于分割结果不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆