Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C how to print out leading 0 for a float?

How do you print out leading zeros for a float using the NSString type?

Input: 3.14
Desired output: 03.1

Using format @"%02.1f"
Output: 3.1
like image 955
Dan Avatar asked Aug 22 '10 23:08

Dan


1 Answers

You want @"04.1f". The 4 is the total width.

As you can see from the documentation, the format strings conform to the IEEE printf specification.

The format string you've specified breaks down as follows:

0 -- Pad with zeros.
2 -- The entire resulting formatted value will have a minimum width of 2.
.1 -- Precision of 1 digit following the decimal point.

like image 168
imaginaryboy Avatar answered Nov 10 '22 23:11

imaginaryboy