将浮点数转换为时间格式mm:ss [英] Convert a float into a time format mm:ss
本文介绍了将浮点数转换为时间格式mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Objective-C搜索一种将浮点数(audioPlayer.currentTime,例如= 3.4592)转换为带有分钟和秒(03:46)的时间字符串的方法。
I'm searching a way with Objective-C to convert a float (audioPlayer.currentTime, which for example = 3.4592) into a time string with minutes and seconds (03:46)
我尝试过:
static NSDateFormatter *date_formatter = nil;
if (date_formatter == nil) {
date_formatter = [[NSDateFormatter alloc] init];
[date_formatter setDateFormat:@"mm:ss"];
}
NSDate *diff = [[NSDate alloc] initWithTimeIntervalSinceNow:audioPlayer.currentTime];
NSLog(@"%@", [date_formatter stringFromDate:diff]);
...但这不起作用。格式正确,但是我找不到正确初始化 diff日期的方法。
... but that's not working. The format is correct but I can't find a way to correctly initialize the "diff" date.
感谢您的帮助!
推荐答案
您可以像这样将float转换为秒
You can convert float in to seconds like this
double d = CMTimeGetSeconds(__avPlayer.currentItem.duration);
然后将其转换为NSString以便在下面的方法中进行演示可能会帮助您
Then its matter of converting that into NSString for presentation below method might help you
- (NSString *) printSecond:(NSInteger) seconds {
if (seconds < 60) {
return [NSString stringWithFormat:@"00:%02d",seconds];
}
if (seconds >= 60) {
int minutes = floor(seconds/60);
int rseconds = trunc(seconds - minutes * 60);
return [NSString stringWithFormat:@"%02d:%02d",minutes,rseconds];
}
return @"";
}
这篇关于将浮点数转换为时间格式mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文