写法一:
/*!
* @brief 格式化时间数值(60秒)为时间格式显示(01:00)
*
* @param seconds 时间数值
* @param isLeft 是否为倒计时
*
* @return String
*/
static NSString * formatTimeInterval(CGFloat seconds, BOOL isLeft)
{
seconds = MAX(0, seconds);
NSInteger s = seconds;
NSInteger m = s / 60;
NSInteger h = m / 60;
s = s % 60;
m = m % 60;
NSMutableString *format = [(isLeft && seconds >= 0.5 ? @"-" : @"") mutableCopy];
if (h != 0) {
[format appendFormat:@"%lu:%0.2lu", h, m];
} else {
[format appendFormat:@"%lu", m];
}
[format appendFormat:@":%0.2lu", s];
return format;
}
//用法:(说明,少了self等调用方式)
NSString *timeString = formatTimeInterval(100,NO);
NSLog(@"右边 = %@",timeString);
//输出:
右边 = 1:40