I’m just beginning to use Python 2.7 and am trying to build a simple credit payment calculator as a test. From what I’ve been able to find, the decimal function should force the program to calculate to a specific number of decimal places. From the documentation pages:
>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')
So my understanding is if I type the following:
from decimal import *
getcontext().prec=2
total =5
print Decimal(total)
I should get the following
5.00
However, it keeps printing 5. If I type Decimal (5) or Decimal (total) I get the output Decimal ('5'). Can anyone tell me what I’m doing wrong?
why dont you just do format strings?
sorry I dont know anything about the decimal package so I really cant help you there…ive never really understood the need, or niche for the decimal package
http://www.shocksolution.com/2011/11/python-string-format-examples/
http://docs.python.org/library/string.html#formatspec