8

I am trying to have a label display single-digit numbers as double-digit numbers (ie. 1 => 01) . I may be using the wrong method to set the value for the label but the thing is I'm not even sure about that after pouring over Apple's documentation and scouring the internet.

[secondsLabel setValue:[NSString stringWithFormat:@"%2d", seconds]]

I saw someone else's use of stringWithFormat as stringWithFormat:@"%.2f" so I figured I would try something similar and change it to "%2d", no luck. Thanks for you help in advance.

b_d
  • 777
  • 2
  • 7
  • 18

1 Answers1

34

You were very close: try "%.2d" (note the ".").

The reference for the format string in the -stringWithFormat: method is the IEEE printf specification

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • 1
    +1 for the dot for leading zeros. I thought they were for post decimal point precision/rounding. I've always used `"%02d"` to achieve the same thing – Madivad Dec 04 '12 at 04:53