I am experiencing a problem when trying to use the getcontext().prec = 2 function, trying to get decimal results with only 2 decimals. I have experimented with several variations, but don’t know why I can’t transform float to string inside one of my lists. I am new using Python (2.6), and have not been able to find a solution for this. Thanks in advance for any help.
from __future__ import division
from decimal import *
getcontext().prec = 2
list_a = ['abc','def','ghi']
list_b = [123, 534, 345]
list_c = [Decimal(str(1/6)), Decimal(str(1/1234)), Decimal(str(2/9))]
for r,c,p in zip(list_a, list_b, list_c):
print '{0} goes with {1} and with {2}%.'.format(r,c,p)
Returns
SyntaxError: Invalid syntax
If I eliminate the str() from the print line, the results are printed, but w/o constraints in the number of decimals. If I use Decimal() – w/o using str first, I get
'first convert the float to a string'.
Any insights?
Cheers
should be
You have an unclosed
)in the code shown in the example.To display only two decimal places: