Raconteur Raconteur - 6 months ago 26
iOS Question

CGFloat to NSString with decimal only if there is fractional part

Not sure how to word the title correctly... but what I am wondering is if there is some clever format specifier that will take the number 4.5 and give me @"4.5" but also take the number 2 and give me @"2".

Using the %.1f specifier gives me @"4.5" but also @"2.0". I am trying to get rid of the ".0" bit.

Does such a beast exist, or am I going to have to do some math on this? FWIW, I am trying to iterate over an array of values ranging from 0 to 5 increasing in half-steps, so 0, 0.5, 1, 1.5, ..., 4.5, 5

Cheers!

Answer

NSNumberFormatter is a good choice here. You can configure it to not show the fractional digits if the number is an integer. For example:

NSArray *numbers = @[@0, @0.5, @1.0, @1.5, @2.0, @2.5];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.alwaysShowsDecimalSeparator = NO;
numberFormatter.minimumFractionDigits = 0;
numberFormatter.maximumFractionDigits = 1;
numberFormatter.minimumIntegerDigits = 1;
for (NSNumber *number in numbers) {
    NSLog(@"%@", [numberFormatter stringFromNumber:number]);
}

Output:

>> 0
>> 0.5
>> 1
>> 1.5
>> 2
>> 2.5