Objective-c:如何四舍五入浮点值?

可能重复:
在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提到的问题。

函数lroundf()将做到这一点:

 float a=20.49; int myInt = lroundf(a); 

转换为int,然后转换回浮动。

 CGFloat *myFloat = 100.765; NSInteger *myInteger = myFloat; CGFloat *newFloat = myInteger; 

这将工作