View Full Version : Displaying float with only 2 decimal places?
Sep 3, 2008, 07:13 AM
Is it possible to display a float variable only out to the 2nd decimal place (hundredths place)?
Sep 3, 2008, 07:15 AM
Look at the documentation for NSString stringWithFormat (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:). You can specify the number of decimal places easily.
Sep 3, 2008, 07:47 AM
Thanks for your help.
Incase anyone else finds this in a search, the way to do it is to use %.2f in the stringWithFormat: method.
Sep 3, 2008, 10:46 AM
Yup - that's a winner...
(more fodder for searches)
The String Programming Guide for Cocoa section on String Format Specifiers (https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html) describes all of the options, but for things like the ".2" part you need to see the referenced IEEE printf() (http://www.opengroup.org/onlinepubs/009695399/functions/printf.html) description.