[Valid RSS]

Chosing the number of decimal digits when printing a number with iPhone

This is a small snippet, useful whenever you want to convert a floating point number in a string with a fixed number of decimal digits.

NSNumberFormatter *format = [NSNumberFormatter new];
 [format setMaximumFractionDigits:1];
 NSNumber *aNumber = [[NSNumber alloc ] initWithDouble:54.4235264];
 NSString label = [[NSString alloc] initWithFormat:@"The number is %@", [format stringForObjectValue:aNumber]];
 [distance release];
 [format release];

Obviously the NSNumberFormatter class offers plenty of other method you can use to customize the way your numbers should be rendered.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>