5

I have an int representing lives.

NSString* lifeString = [NSString stringWithFormat:@"%d",lives];

However it outputs:

1
2
3
..
10
...

I need:

    01
    02
    03
    ..
    10
    ...

I know I can do this with an if statement, but I am wondering if NSString might have a clever way to do it.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • Remember to look at the list of Related questions before posting your question. The first one listed would have answered your question. – rmaddy Nov 17 '13 at 00:59

1 Answers1

20

Use %02d as your format code to require a particular length (i.e. 2), and the left padding value of zero:

NSString* lifeString = [NSString stringWithFormat:@"%02d",lives];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523