Python‎ > ‎decimal‎ > ‎

Decimal.quantize()


Introduction
 Return a value equal to the first operand after rounding and having the exponent of the second operand.

Syntax
Decimal(x).quantize(exp, rounding=None, context=None) ->quantize Decimal

Parameters
x= Decimal

Examples
The return are:
 >>> Decimal(1.000009).quantize(Decimal(1.0))
Decimal('1')

>>> Decimal(1.00000000000000000001).quantize(Decimal(1.00))
Decimal('1')

>>> Decimal('1.0000000000000000000').quantize(Decimal('1.000'))
Decimal('1.000')

list.append(decimal)
The method list the decimal value and limit the decimal point to '1.00' through quantize.
from decimal import Decimal
list1=list()
list.append((Decimal(2.10).copy_negate().quantize(Decimal('1.00')),
                                 Decimal(1.3).copy_sign(Decimal(1.3)).quantize(Decimal('1.00')),
                                    Decimal(3).exp().quantize(Decimal('1.00')))
)
print("The items in the list1 are:,", list1)

>>> 
The items in the list1 are:, [(Decimal('-2.10'), Decimal('1.30'), Decimal('20.09'))]
>>>



Comments