-1

I am trying to implement a chronometer on my app, but the left side zeros won't show up. Do you know any way to make them default?

Here's what I am trying:

self.text.string = [NSString stringWithFormat:@"%2.0d:%2.0d:%1.0d", minute, second, decimals];

Here's what I really see:

2014-01-04 15:55:06.462 appchrono[17556:70b]   :3:2
2014-01-04 15:55:06.562 appchrono[17556:70b]   :3:3

Here's what I want it to be:

2014-01-04 15:55:06.462 appchrono[17556:70b]   00:03:2
2014-01-04 15:55:06.562 appchrono[17556:70b]   00:03:3

Any help? Thank you!

sch
  • 27,436
  • 3
  • 68
  • 83
RickON
  • 395
  • 7
  • 18

2 Answers2

1

You're close:

 self.text.string = [NSString stringWithFormat:@"%02d:%02d:%02d", minute, second, decimals];
godel9
  • 7,340
  • 1
  • 33
  • 53
0

Try %-2.2d. This tells the formatter to prefix zeroes.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
uulhaa
  • 121
  • 3
  • May not make sense, but it works. – uulhaa Jan 04 '14 at 18:12
  • I just tried this and to my surprise this does actually give the right result. However, the more correct format would be `%02d`. The fact that `%-2.2d` actually works is pretty strange on very non-obvious. – rmaddy Jan 04 '14 at 18:27
  • You are very right. But I just remembered this from my printf days (1985...). In those day %02 usually did not work (depending on the actual OS being used), so we used the other. – uulhaa Jan 04 '14 at 18:33