Viewing and changing parameters

The decimal module has parameters that control how it works with and displays numbers. To see the parameters, type the following:

>>> import decimal >>> decimal.getcontext()

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

One of the parameters is the maximum precision of decimal numbers (how many digits to the right of the decimal point you see). To change the precision parameter, type something like this:

Tip If you want to change the precision of a specific number without changing the precision parameter for all the numbers, use the quantize() method. quantize() rounds the number to a specific number of decimal places. This example rounds to two decimal places:

Decimal("7.32")

Was this article helpful?

0 0

Post a comment