Showing left side zeros formating strings - Objective-C

138 Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

You're close:

 self.text.string = [NSString stringWithFormat:@"%02d:%02d:%02d", minute, second, decimals];
3
On

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