76

I'm trying to do some number rounding and conversion to a string to enhance the output in an Objective-C program.

I have a float value that I'd like to round to the nearest .5 and then use it to set the text on a label.

For example:

1.4 would be a string of: 1.5

1.2 would be a string of: 1

0.2 would be a string of: 0

I've spent a while looking on Google for an answer but, being a noob with Objective-C, I'm not sure what to search for! So, I'd really appreciate a pointer in the right direction!

Thanks, Ash

dreamlax
  • 93,976
  • 29
  • 161
  • 209
Ash
  • 2,330
  • 2
  • 18
  • 23
  • 27
    Except for adding the value to a label (which he's declaring as his intent and not actually asking about) his question has nothing to do with Cocoa and everything to do with Obj-C – Jason Coco Apr 15 '09 at 23:19

11 Answers11

100

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!

Ash
  • 2,330
  • 2
  • 18
  • 23
  • 7
    If this is being output to a text field, you can just attach the formatter to the field. – Chuck Apr 15 '09 at 22:17
40
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);
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
33
NSString *numberString = [NSString stringWithFormat:@"%f", round(2.0f * number) / 2.0f];
keremk
  • 2,303
  • 14
  • 10
25

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

  • 1
    This seems a bit confusing, so I clarify. `lroundf()` rounds a float to a whole number and gives it back as long integer (to convert it to a string you can use `stringWithFormat:`). – Daniel Nov 17 '14 at 14:01
11
NSString *numberString = [NSString stringWithFormat:@"%d",lroundf(number)];
Simpu
  • 703
  • 6
  • 3
8

I'd recommend looking into using NSNumberFormatter.

hbw
  • 15,560
  • 6
  • 51
  • 58
6

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
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
2
NSString *intNumberString = [NSString stringWithFormat:@"%d", (int)floatNumber];
Vladimirs Matusevics
  • 1,092
  • 12
  • 21
2

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.

Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
2

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;
}
JJussi
  • 1,540
  • 12
  • 12
1

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];
Community
  • 1
  • 1
Stunner
  • 12,025
  • 12
  • 86
  • 145