gpt4 book ai didi

swift - 将秒数转换为时间戳

转载 作者:行者123 更新时间:2023-11-28 15:15:46 28 4
gpt4 key购买 nike

我正在使用 Swift 计算一个时间数组,以达到百分之一秒内的平均次数。得到的平均值是总的第一百秒的输出。

例如:总计第 111 秒

如何将这百分之一秒转换为

(分钟):(秒)。 (第一百秒)

(最多 60 个):(最多 60 个)。 (最多 100 个)

例如:00:01.11

我正在使用它来将数组转换为总百分秒:

let timeToSecondsArray = valueLast20.map{
i -> Int in
let timeString = (i as AnyObject).components(separatedBy: ":")
let minute = Int(timeString[0])
let second = Int(timeString[1])
let hundredthseconds = Int(timeString[2])
let minuteToSec = minute! * 3600
let secondToSec = second! * 60
let totalSeconds = minuteToSec + secondToSec + hundredthseconds!
return totalSeconds
}

let seconds: Int = timeToSecondsArray.reduce(0, {x, y in x + y})/timeToSecondsArray.count
let one = String(format: "%02ld", seconds/3600)
let two = String(format: "%02ld", (seconds%3600)/60)
let three = String(format: "%02ld", ((seconds%3600)%60))
self.averageTime.text = "\(one):\(two).\(three)"

最佳答案

let valueLast20 = ["01:03:24", "01:00:98"]

let hundredthSecondsArray = valueLast20.map{
i -> Int in
let timeString = (i as AnyObject).components(separatedBy: ":")
let minute = Int(timeString[0])
let second = Int(timeString[1])
let hundredthSeconds = Int(timeString[2])!
let minuteToHundredth = minute! * 60 * 100
let secondToHundredth = second! * 100
let totalSeconds = minuteToHundredth + secondToHundredth + hundredthSeconds
return totalSeconds
}

let hundredth: Int = hundredthSecondsArray.reduce(0, +) / hundredthSecondsArray.count
let one = String(format: "%02ld", hundredth / 100 / 60)
let two = String(format: "%02ld", (hundredth / 100 % 60))
let three = String(format: "%02ld", (hundredth % 100))
print("\(one):\(two).\(three)")

01:02.11

关于swift - 将秒数转换为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46896832/

28 4 0
文章推荐: html - Bootstrap 面板不适用于某些颜色
文章推荐: html - Flexbox:溢出自动和窗口大小
文章推荐: html - 在html和css中制作方框
文章推荐: html - 无法在 css 中的