我正在使用Swift计算一个平均次数的数组,以百分之一秒为单位。得到的平均值是总百分之一秒的输出。
ex:总计百分之111
如何将这些百分之一秒转换为
(分钟):(秒)。 (百分之一秒)
(最多60):(最多60)。 (最多100个)
ex: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)"
答案 0 :(得分:1)
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