-2

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?

Voldemort
  • 17,552
  • 47
  • 139
  • 262
  • 3
    Was there even an *attempt* to find some documentation first? –  Sep 10 '11 at 01:42
  • possible duplicate of [how to set the float value to two decimal number in objective C](http://stackoverflow.com/questions/560517/how-to-set-the-float-value-to-two-decimal-number-in-objective-c) or [How to format float number](http://stackoverflow.com/questions/2134260/how-to-format-float-number-objective-c) @pst – jscs Sep 10 '11 at 01:50
  • @pst: No, there wasn't any attempt as far as I can recall. Why? – Voldemort Dec 16 '12 at 21:42

4 Answers4

10

%0.2f or some other number after the decimal point.

Wevah
  • 28,020
  • 7
  • 84
  • 72
4
NSLog(@"%0.1f", floatThing);

I think that should work. For more decimals change 0.1 to 0.2, 0.3, 0.4 ... etc.

Dair
  • 15,412
  • 8
  • 59
  • 104
1
[NSString stringWithFormat:@"%.DECIMAL_PRECISION_NUMBER_HEREf", someFloat]
animuson
  • 52,378
  • 28
  • 138
  • 145
chown
  • 50,544
  • 16
  • 131
  • 169