-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?

Saturn
  • 17,888
  • 49
  • 145
  • 271
  • 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? – Saturn Dec 16 '12 at 21:42

4 Answers4

10

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

Wevah
  • 28,182
  • 7
  • 83
  • 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,910
  • 9
  • 62
  • 107
1
[NSString stringWithFormat:@"%.DECIMAL_PRECISION_NUMBER_HEREf", someFloat]
animuson
  • 53,861
  • 28
  • 137
  • 147
chown
  • 51,908
  • 16
  • 134
  • 170