Can anyone tell me what happened when str(time.time()) runs? It seems the data has been truncated, but it recovered when float(str(time.time())) runs. I'm so confused.
In [1]: import time
In [2]: a = time.time()
In [3]: a
Out[3]: 1445007755.321532
In [4]: str(a)
Out[4]: '1445007755.32'
In [6]: a.__str__()
Out[6]: '1445007755.32'
In [7]: float(a)
Out[7]: 1445007755.321532
Calling str(a) does not modify a, so there is no reason why float(a) should depend on str(a). Maybe you wanted to reassign a?
a = 1445007755.321532
a = str(a)
float(a)
In this case the result is different:
>>> a = 1445007755.321532
>>> b = str(a)
>>> a == float(b)
False
>>> a, b, float(b)
(1445007755.321532, '1445007755.32', 1445007755.32)
Note that it holds that float(x) == x if x is already a float, so what you see is not surprising at all.
Even more: float(x) is x if x is of type float.
If you wonder why str seems to truncate the number it is because it returns a "human readable" representation of the value as a string.
If you want an "exact" representation use repr, or simply use a proper formatting function like: "%.6f" % a to control the number of decimal digits etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With