目标 C 中的四舍五入数

我正在尝试进行一些数字舍入和字符串转换,以增强 Objective-C 程序的输出。

我有一个浮点值,我想四舍五入到最接近的。5,然后使用它来设置标签上的文本。

例如:

1.4是一个字符串: 1.5

1.2是一个字符串: 1

0.2将是一个字符串: 0

我花了一段时间在谷歌上寻找答案,但是,作为一个使用 Objective-C 的菜鸟,我不知道该搜索什么!所以,我真的很希望有一个正确的方向的指针!

谢谢, 阿什

101305 次浏览

I'd recommend looking into using NSNumberFormatter.

NSString *numberString = [NSString stringWithFormat:@"%f", round(2.0f * number) / 2.0f];

Thanks for the pointers everyone, I've managed to come up with a solution:

float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];


NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];

The above works for the test cases I threw at it, but if anyone knows a better way to do this I'd be interested to hear it!

Use lroundf() to round a float to integer and then convert the integer to a string.

NSString *numberString = [NSString stringWithFormat:@"%d",lroundf(number)];
NSString *intNumberString = [NSString stringWithFormat:@"%d", (int)floatNumber];
float floatVal = 1.23456;

Rounding

int roundedVal = lroundf(floatVal);


NSLog(@"%d",roundedVal);

Rounding Up

int roundedUpVal = ceil(floatVal);


NSLog(@"%d",roundedUpVal);

Rounding Down

int roundedDownVal = floor(floatVal);


NSLog(@"%d",roundedDownVal);

Following Technique worked for me in a financial application.

 NSString *dd=[NSString stringWithFormat:@"%0.2f",monthlyPaymentCalculated];


monthlyPaymentCalculated=[dd doubleValue];


self.monthlyPaymentCritical=monthlyPaymentCalculated;

what is did is first is rounded it with %0.2f and stored it in NSString then i simply converted it again into double and result was good for my calculation.

I needed to be able to round to a specific digit (not necessarily to whole integers). I made a NSNumber category (based off Ash's answer) and added the following method to it:

- (NSString *)stringByRounding:(NSNumberFormatterRoundingMode)roundingMode
toPositionRightOfDecimal:(NSUInteger)position
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:position];
[formatter setRoundingMode:roundingMode];
NSString *numberString = [formatter stringFromNumber:self];
return numberString;
}

Which allows me to use it like so:

[aNumber stringByRounding:NSNumberFormatterRoundUp toPositionRightOfDecimal:2];

I can use it to round to integers by passing in 0 as the second parameter:

[aNumber stringByRounding:NSNumberFormatterRoundPlain toPositionRightOfDecimal:0];

With these functions you can round to any value.. If you use p=2, you get even numbers.

float RoundTo(float x, float p)
{
float y = 1/p;
return int((x+(1/(y+y)))*y)/y;
}


float RoundUp(float x, float p)
{
float y = 1/p;
return int((x+(1/y))*y)/y;
}


float RoundDown(float x, float p)
{
float y = 1/p;
return int(x*y)/y;
}

a simple way:

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