我无法让DateComponentsFormatter
打印出小数单位!
以下是 Swift 4 中的代码:
let duration = 5400 // 1.5 hours in seconds
let dateComponentsFormatter = DateComponentsFormatter.init()
dateComponentsFormatter.allowedUnits = [.hour]
dateComponentsFormatter.maximumUnitCount = 1
dateComponentsFormatter.allowsFractionalUnits = true
dateComponentsFormatter.unitsStyle = .short
let stringDuration = dateComponentsFormatter.string(from: number)
print(stringDuration!)
我玩allowedUnits
和maximumUnitCount
,但我只能打印“1小时”或“2小时”。
如何制作“1.5小时”?