美文网首页iOS Developer
Swift TimeInterval 转换为 Int,秒数&am

Swift TimeInterval 转换为 Int,秒数&am

作者: WonderChang | 来源:发表于2016-09-21 16:27 被阅读4281次

参考:http://stackoverflow.com/questions/11121459/how-to-convert-nstimeinterval-to-int

  1. 简单粗暴法:
let interval: TimeInterval = NSDate.timeIntervalSinceReferenceDate;
print("interval: \(interval)")
let time = Int(interval)
print("time: \(time)")
//结果为:interval: 496138595.969168
//time: 496138595

这样是直接截断,不太友好。

  1. 四舍五入法:
    可精确到秒或毫秒:
  let aInterval = 496138080.543964
    //精确到秒
    let second = Int(round(aInterval))
    //精确到毫秒
    let millisecond = CLongLong(round(aInterval*1000))

    print("second: \(second), millisecond: \(millisecond)")
//结果为:second: 496138081, millisecond: 496138080544

相关文章

网友评论

    本文标题:Swift TimeInterval 转换为 Int,秒数&am

    本文链接:https://www.haomeiwen.com/subject/zybjettx.html