Convert Unix timestamp using float conversion precision problem


(Rob Marshall) #1

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


(Norbert Melzer) #2

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


(Rob Marshall) #3

That’s what I figured…Thanks.


(Rob Marshall) #4

There are probably better ways to do this:

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

Rob