将float舍入到.5处的下一个整数

时间:2014-07-24 17:22:31

标签: ios objective-c floating-point rounding

我以为lroundf会将我的浮动围绕到.5步的下一个最高数字。例如。 1.5f将四舍五入为2.0f

我试过,但我的代码告诉我:

int roundUp = 1.5f;

NSLog(@"no round up: %d", roundUp);
NSLog(@"lroundf: %ld", lroundf(roundUp));

我的输出是:

no round up: 1
lroundf: 1

如何正确收集我的浮动?

1 个答案:

答案 0 :(得分:1)

int rounded = lroundf(theFloat); NSLog(@"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(@"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(@"%d",roundedDown);