iphone - Objective-C 浮点舍入

标签 iphone objective-c ios floating-point rounding

如何在 Objective-C 中将 float 舍入到最接近的整数:

例子:

float f = 45.698f;
int rounded = _______;
NSLog(@"the rounded float is %i",rounded);

应该打印“四舍五入的 float 是 46”

最佳答案

使用 C 标准函数系列 round()roundf() 用于 floatround() 用于 doubleroundl() 用于 long double。然后,您可以将结果转换为您选择的整数类型。

关于iphone - Objective-C 浮点舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702408/

相关文章:

ios - 在 iOS 中从头开始创建视频

ios - 在加载应用程序之前检查状态栏是否已经在 iPhone 上可见

iphone - 隐藏或删除 UIImageView 和排列 (IOS 5)

ios - Apple 是否在 Swift 中弃用了 iPhone 3D touch?

iphone - 对象被过早释放?

iphone - popViewController动画:YES crashes my app

ios - 如何在 TableView Cell 上使用 iOSCharts (https ://github. com/danielgindi/Charts)?

ios - 自定义分段控件宽度代码没有故意工作

ios - 如何将 FMDB 导入 swift 框架

ios - 通过 XIB 文件在 tableView 到DetailedTableView 之间导航