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?

Answers


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.


Need Your Help

Control tint color in UISegmentedControl

ios tint

In most of my projects a UX designer will provide me a RGB color for an asset in iOS, but am having trouble providing what is needed for a tint color in a control. To explain, since the tint ends up

How is this plugin designed to crash IE6, crashing IE6?

jquery browser jquery-plugins internet-explorer-6 crash

Chad Smith came out with a jQuery plugin designed to crash the IE6 browser.