Create an NSString with leading zeroes for minutes and seconds
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?
NSString *time = [NSString stringWithFormat:@"%02d:%02d", (int)min, (int)sec];
Try this. Should just work!
This should do the trick:
int seconds = (int)currentProgress % 60; int minutes = currentProgress / 60; NSString *result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
The %02d format specifier will automatically pad with 0s so that the field as at least 2 characters wide.