如何将float
转换为int
,同时舍入到下一个整数?例如,1.00001会去2和1.9999会去2.在Objective-C中将浮点数转换为int
10
A
回答
61
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;
+0
谢谢!它为我工作。 – Raja 2017-05-03 06:07:56
11
使用ceil功能:
int intValue = (int)ceil(yourValue);
1
您可以使用下面的C方法从不同的数据类型得到int类型。
extern float ceilf(float);
extern double ceil(double);
extern long double ceill(long double);
这些函数分别返回float,double和long double。但是这些功能的工作是获得论证的底层或底层。正如http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
然后你可以将返回值转换为所需的类型。
int intVariable = (int)ceilf(floatValueToConvert);
希望它有帮助。
0
如果我们有像13.123浮点值将其转换成整数像13
代码
浮子floatnumber = 13.123; //你也可以使用CGFloat的,而不是浮
NSLog(@"%.f",floatnumber); // use for print in command prompt
相关问题
- 1. 转换浮点数为int
- 2. 将int转换为浮点数F#
- 3. STM32F4浮点数转换为int转换
- 4. 将int位转换为浮点位
- 5. 将pandas浮点系列转换为int
- 6. 如何将int转换为浮点VB6
- 7. 在C++中将int转换为16位浮点数(半精度浮点数)
- 8. 将浮点数转换为int,有无转换
- 9. Java从浮点数转换为int
- 10. 在Swift中将浮点数转换为浮点数
- 11. 将int转换为C中的浮点数
- 12. 如何用C中的模值将浮点数转换为Int?
- 13. 如何将浮点数转换为Objective C中的int值?
- 14. 如何在objectiveC中将NSUInteger值转换为int值?
- 15. 在HQL中将浮点数转换为浮点型
- 16. 将字符串转换为int或浮点在Python 3中?
- 17. 将字符中的浮点数转换为浮点数(PYTHON)
- 18. 浮点无需转换诠释为int
- 19. 错误转换为浮点数,然后在Python中int
- 20. 熊猫irow()自动将int转换为浮点数
- 21. 如何将int整型应答转换为浮点数
- 22. 将一个int变量转换为浮点数
- 23. 试图将浮点数转换为int时出现问题
- 24. 如何将浮点数转换为int(sympy(Python))?
- 25. 如何将浮点数转换为int保留位值
- 26. PHP - 从大型浮点数转换为int将返回0
- 27. 将32位int值转换为浮点数
- 28. 将float浮点数转换为int结果的值不正确
- 29. 熊猫将int值转换为浮点数据框
- 30. MIPS将浮点数转换为整数
请逆向工程&这里http://stackoverflow.com/questions/286756/how-do-i-convert-a-float-to-an-int-in -objective-c – wanmuz 2012-01-09 08:16:07
你为什么使用float而不是double?另一个问题,你有没有想过要买一本关于C语言的书?每一个听到的功能层,小区和圆? – gnasher729 2014-05-29 07:45:03