gpt4 book ai didi

iOS - 如何将 float 时间转换为实时时间 (hh :mm)

转载 作者:行者123 更新时间:2023-11-28 20:16:03 25 4
gpt4 key购买 nike

我使用以下计算方法计算从一个点到另一个点的平均旅行时间:

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com