need to print seconds from the epoch including the millisecond
roy at panix.com
Mon Dec 30 15:22:21 CET 2013
In article <mailman.4714.1388407860.18130.python-list at python.org>,
Ned Batchelder <ned at nedbatchelder.com> wrote:
> A float's str() includes two decimal points of precision
It's actually weirder than that. What str() appears to do is print some
variable number of digits after the decimal place, depending on the
magnitude of the number, and then flips over to exponential notation at
some point. All of this is in line with str()'s intent, which is to
produce a human-friendly string:
for i in range(15):
f = 10**i + 0.123456789
print "%-20s %-20s" % (str(f), repr(f))
$ python float.py
It just happens that for the range of values time.time() is returning
these days, two decimal digits is what you get. Unix time rolled over
to this many digits on
'Sat Sep 8 21:46:39 2001'
so before then, str(time.time()) would have (presumably) generated 3
Note that repr() also adjusts the number of digits after the decimal
place, but this is because it's run out of available hardware precision
(IEEE double precision is about 16 decimal digits).
Also note that while repr() is smart enough to stop when it runs out of
bits, the %f format specifier isn't:
>>> f = 10000000000000.123456789
>>> "%.6f" % f
> Luckily, you can decide how to format the float yourself:
> >>> print "%.4f" % time.time()
The problem here is that you have no guarantee here that all those
digits are meaningful. I'm not sure what would happen on a machine
where the system clock only gives centisecond precision.
I would like to think "%.4f" % time.time() would always produce a string
with 4 digits after the decimal point, the last two of which were
guaranteed to be "0". But not having such a box handy to test, that's
just a conjecture. A much more pathological case would be that it
produces random garbage for the extra digits, which would make it appear
that you were getting more time precision than you really were.
All of which is a good reason to avoid raw timestamps and use datetime.
With a datatime object, somebody else has already worried about these
things for you.
PS: all the above examples were done with Python 2.7.1 on OSX 10.7.
More information about the Python-list