Create an NSString with leading zeroes for minutes and seconds

cocoa, cocoa-touch, formatting, objective-c, string

Solution

NSString *time = [NSString stringWithFormat:@"%02d:%02d", (int)min, (int)sec];

Try this. Should just work!

Problem

I need to constantly update the a playback time in a label. I am getting the time `NSTimeInterval currentProgress = self.moviePlayerController.currentPlaybackTime;` which returns a double value. But the format in which I need to display is "MM:SS" (00:00). I have tried the following code ``` NSTimeInterval currentProgress = self.moviePlayerController.currentPlaybackTime; float min = floor(currentProgress/60); float sec = round(currentProgress - min * 60); ``` When the progress time is 10 minutes and 10 seconds it displays perfectly fine, as "10:10". But the problem here is if the progress time is 1 minute and 7 seconds, it displays "1:7" in the label. My intended display is "01:07". How can I do that?

Original source