开发者

how can i define NSTimeInterval to mm:ss format in iphone?

开发者 https://www.devze.com 2022-12-25 02:42 出处:网络
how can i define NSTimeInterv开发者_运维问答al to mm:ss format?NSTimeInterval interval = 326.4;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号