Possible Duplicate:
Rounding numbers in Objective-C

在Objective-c中,如何舍入浮点值?

推荐答案

除了其他答案外:

float theFloat = 1.23456;
int rounded = roundf(theFloat); NSLog(@"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(@"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(@"%d",roundedDown);
// Note: int can be replaced by float

有关舍入到特定小数的信息,请参见Alex Kazaev提到的问题.

Objective-c相关问答推荐

多个 WKWebView 之间的 Cookie 共享

如何在 Cocoa 中创建字符串的 MD5 哈希?

在 Interface Builder 中设计 UITableView 的section header

UISearchbar 键盘搜索按钮操作

将 CFDictionaryRef 转换为 NSDictionary?

Objective C 代码是否有 github markdown 语言标识符?

带有 IB_DESIGNABLE 的 UIButton 会引发运行时属性警告,并且不会在 Interface Builder 中呈现

UITableView 无限滚动

从 NSString 创建 SHA1 哈希

是否可以在 nsstring 中包含引号?

`-fembed-bitcode` 和 BITCODE_GENERATION_MODE 有什么区别?

在 iPhone 上测量人体胀气( Human-Flatulence)传播波包

应用程序内部的Gift App

了解按位与运算符

Xcode 在 iOS 8 中的 Main() 中引发异常,并带有所有异常断点

在 Objective C 中,你能判断一个对象是否具有特定的属性或消息吗?

通过索引访问 NSMutableDictionary 中的对象

如何为 iPhone 应用程序创建多个主题/皮肤?

捕捉固定步数的 UISlider(如 iOS 7 设置应用程序中的文本大小)

Block 隐式保留'self';明确提及touch以表明这是预期行为