Trying to set precision with the decimal package but unable to obtain desired precision

Viewed 142

jupyter nbI'm playing around with the decimal package in Python and wondering why the precision I'm setting is not implemented as desired. For example:

from decimal import *
getcontext()

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])


getcontext().prec = 2
getcontext()

Context(prec=2, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

Decimal(1)/Decimal(9)
Decimal('0.1111111111111111111111111111')

I was expecting that the output from the line Decimal(1)/Decimal(9) would be Decimal('0.11') rather than Decimal('0.1111111111111111111111111111'). What am I missing?

0 Answers
Related