NSDateFormatter timeIntervalSince1970奇怪的行为,以毫秒为单位(NSDateFormatter timeIntervalSince1970 strange beha

编程入门 行业动态 更新时间:2024-10-19 22:41:33
NSDateFormatter timeIntervalSince1970奇怪的行为,以毫秒为单位(NSDateFormatter timeIntervalSince1970 strange behavior with milliseconds)

我正在尝试使用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.

更多推荐

本文发布于:2023-08-05 06:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   单位   NSDateFormatter   behavior   strange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!