我使用以下计算方法计算从一个点到另一个点的平均旅行时间:
float tSpeed = [[NSString stringWithFormat:@"%f", speed] floatValue];
float duration = totDistance/tSpeed;
tripDuration_Label.text = [NSString stringWithFormat:@"%f", duration];
速度以英里/小时为单位,因此输出以小时为单位。
这为我提供了时间的浮点值,我需要将其转换为时间 (hh:mm)。
谢谢
最佳答案
1 小时是 60 分钟,所以:
NSInteger hours = duration; // 2.5 -> 2
NSInteger minutes = ( duration - hours ) * 60; // ( 2.5 - 2 ) * 60
tripDuration_Label.text = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
关于iOS - 如何将 float 时间转换为实时时间 (hh :mm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18075315/