String formatting: less decimals in %f float? [duplicate]
Well, I am using %f
for displaying float values in my string. However, the values are too accurate (around 6 decimal digits after the point). I'd like to only display 1 decimal. But how?
%0.2f
or some other number after the decimal point.
NSLog(@"%0.1f", floatThing);
I think that should work. For more decimals change 0.1 to 0.2, 0.3, 0.4 ... etc.
[NSString stringWithFormat:@"%.DECIMAL_PRECISION_NUMBER_HEREf", someFloat]
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
and
http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
精彩评论