Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Formatting numbers with "just enough" decimal places

Tags:

objective-c

Is there a quick way to format numbers (e.g., when using appendFormat) so that only enough decimal places are shown? E.g., 4.23100 is shown as 4.231, while 5.0000 is shown as just 5.

Thanks for reading.

like image 613
Rogare Avatar asked Nov 03 '12 21:11

Rogare


1 Answers

Use %g instead of %f

double n = 1234.5678;

NSString str = [NSString stringWithFormat:@"number is %g", n];

like image 139
Roger Gilbrat Avatar answered Nov 15 '22 06:11

Roger Gilbrat