kaks kaks - 2 months ago 5
Python Question

When accessing single element from list of float values, value get's rounded off

My list contains values which are converted to float :

time = [1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]


But when i do
time[0]
, the value gets rounded off to 2 decimal places like
1472120400.11
. How do I retain the original values (upto 3 decimal places for accurate calculations) when accessing single list values?

My code:

for line in line_collect :
a = re.search(rx1,line)
time = a.group()
newlst.append(float(time))
print newlst
print newlst[0]


Output:


[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]

1472120400.11

Answer

The list itself doesn't have rounded values. It is the default "print" that shows rounding:

>>> time = [1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]
>>> time[0]
1472120400.107
>>> print time[0]
1472120400.11

You can get full precision output by printing the repr instead:

>>> print repr(time[0])
1472120400.107
Comments