我正在尝试使用NSDateFormatter解析一个毫秒的日期,当我打印出自1970年以来的时间间隔时,我得到了奇怪的结果。这是我的测试代码:
var timeStr = "2015-09-29 14:32:42.297-07" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSx" let date = dateFormatter.dateFromString(timeStr)! let dateFormatted = String(format: "%.20f", date.timeIntervalSince1970) print("date: \(dateFormatted)")结果: "date: 1443562362.29699993133544921875\n"
它不是以297结尾,而是以接近它的东西结束。 为什么?
I'm trying to parse a date with milliseconds using NSDateFormatter, and I'm getting weird results when I print out its time interval since 1970. Here's my test code:
var timeStr = "2015-09-29 14:32:42.297-07" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSx" let date = dateFormatter.dateFromString(timeStr)! let dateFormatted = String(format: "%.20f", date.timeIntervalSince1970) print("date: \(dateFormatted)")Result: "date: 1443562362.29699993133544921875\n"
Instead of ending with 297, it ends with something close to it. Why?
最满意答案
似乎这只是双打工作的方式。 它与NSDateFormatter无关。 如果我制作一个值为2.3的双倍然后打印到20位小数,我得到2.99999990453636 ...或者其他东西。
Seems this is just how doubles work. It's not related to NSDateFormatter. If I make a double with value 2.3 then print it to 20 decimal places, I get 2.99999990453636... or something.
更多推荐
发布评论