objective-c - 将秒整数转换为 HH :MM, iPhone

我正在为此苦苦挣扎。我有一个以秒为单位的值,我想以 HH:MM 格式显示在标签中。我已经在互联网上搜索了很长时间并找到了一些答案,但要么没有完全理解它们,要么它们看起来像是一种奇怪的方式来做我想做的事。如果有人可以帮助我解决这个问题,那就太好了!请记住,我是这个游戏的新手,所以对于更有经验的人来说,这个问题似乎是一个非常基本的问题。

最佳答案

我一直在寻找与您相同的东西,但找不到。所以我写了一个 -

- (NSString *)timeFormatted:(int)totalSeconds
{

    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds / 60) % 60; 
    int hours = totalSeconds / 3600; 

    return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds]; 
}

Swift 中也能完美运行:

 func timeFormatted(totalSeconds: Int) -> String {
    let seconds: Int = totalSeconds % 60
    let minutes: Int = (totalSeconds / 60) % 60
    let hours: Int = totalSeconds / 3600
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
 }

https://stackoverflow.com/questions/1739383/

相关文章:

c++ - 我应该在我的 C++ 代码中使用 printf 吗?

xml - 在 Eclipse 中格式化 XML 代码

.net - .NET 中是否有一种简单的方法来获取数字的 "st"、 "nd"、 "rd"和 "t

c++ - 如何使用 ostream 在 c++ 中将 unsigned char 打印为十六进制?

objective-c - 将字符串转换为 NSDate

visual-studio - 如何在 Visual Studio (Express) 2008 中

intellij-idea - 如何在 WebStorm 中自动格式化代码?

c# - 如何使 Visual Studio 不将 { 放在新行上?

java - 如何将 SimpleDateFormat 与日历一起使用?

c# - 文件大小格式提供程序