本文介绍了在Objective-C中将float转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在四舍五入到下一个整数时将 float
转换为 int
?例如,1.00001 会去 2,1.9999 会去 2.
How can I convert a float
to int
while rounding up to the next integer? For example, 1.00001 would go to 2 and 1.9999 would go to 2.
推荐答案
float myFloat = 3.333
// for nearest integer rounded up (3.333 -> 4):
int result = (int)ceilf(myFloat );
// for nearest integer (3.4999 -> 3, 3.5 -> 4):
int result = (int)roundf(myFloat );
// for nearest integer rounded down (3.999 -> 3):
int result = (int)floor(myFloat);
// For just an integer value (for which you don't care about accuracy)
int result = (int)myFloat;
这篇关于在Objective-C中将float转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!