Convert Unix timestamp using float conversion precision problem

Hi,

I’m trying to convert a Unix timestamp that is a float to a time.Time but running into precision issues. For example, if I have a timestamp of: 1564670787.9459848 and I do a strong.ParseFloat() and then give that to time.Unix().UnixNano() I get back: 1564670787945984768 which is incorrect. Is there a way around this? Other than splitting the value as a string and dealing with the parts as ints. Here’s my example:

https://play.golang.org/p/0SRerY9zcct

Thanks,

Rob

2 Likes

This is as correct as a float is representable in memory.

2 Likes

That’s what I figured…Thanks.

2 Likes

There are probably better ways to do this:

https://play.golang.org/p/n72yvEs3332

Rob

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.