Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
CGFloat result=100/126; NSLog(@"%0.5f",result);
上面代码的输出是 0.00000,但是当我在计算器上检查它时,它的值是 0.79365。问题是什么
问题在于除法的两个操作数都是整数,因此执行整数除法,结果为 0。要获得正确的值,您需要确保至少有一个操作数是浮点数:
CGFloat result=100.0f/126;
May be because your variables are integers. Do this
CGFloat result=100.0/126.0; NSLog(@"%0.5f",result);