1

I have an array:

a = [0.313767818199811, 0.169656461527218, 0.934219696497667]

When I do

print a
[0.313767818199811, 0.169656461527218, 0.934219696497667]

print a[0]
0.3137678182

I need to preserve all the digits of each number in an array. I need a[0] to equal 0.313767818199811

Whats the best way to tell python to preserve the digits?

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005

4 Answers4

4

a[0] does equal the full expansion. The print statement is truncating it (using str) for display purposes.

The following code might make this clearer. In particular, it explains the discrepancy between printing a and a[0]str(a) calls repr (not str) on the elements of a:

>>> str(a)
'[0.313767818199811, 0.169656461527218, 0.934219696497667]'
>>> str(a[0])
'0.3137678182'

>>> repr(a)
'[0.313767818199811, 0.169656461527218, 0.934219696497667]'
>>> repr(a[0])
'0.313767818199811'

As suggested in the above code, you can display a[0] with the full precision as follows:

>>> print repr(a[0])
0.313767818199811
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
3

You can use:

print '%.15f' % a[0]
>> 0.313767818199811

It will display 15 digits after the decimal point.

Nicolas
  • 5,583
  • 1
  • 25
  • 37
0

you may need repr() here, print uses str() so your output is truncated:

In [13]: repr(a[0])
Out[13]: '0.313767818199811'

In [14]: repr(a[1])
Out[14]: '0.169656461527218'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

Use formatting: print '%.17f' % a[0] for 17 digits. If you ever have troubles with this again, google 'python float print precision'

Lucas Hoepner
  • 1,437
  • 1
  • 16
  • 21