how can i define NSTimeInterv开发者_运维问答al to mm:ss format?
NSTimeInterval interval = 326.4;
long min = (long)interval / 60; // divide two longs, truncates
long sec = (long)interval % 60; // remainder of long divide
NSString* str = [[NSString alloc] initWithFormat:@"%02d:%02d", min, sec];
The %02d format specifier gives you a 2 digit number with a leading zero.
Note: this is for positive values of interval only.
See this question.
Accepted answer by Brian Ramsay is:
Given 326.4 seconds, pseudo-code:
minutes = floor(326.4/60)
seconds = round(326.4 - minutes * 60)
If you print with %02d
, you will get e.g. 03:08
if either number is less than 10.
精彩评论