Python standard library - decimal numbers and mathematical module of Introduction to use

Standard library - numbers and mathematical module of the decimal use Description

by: award-off QQ : 1033553122

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])

 

>>>Decimal('5')/3

Decimal('1.666666666666666666666666667')

 

>>> getcontext (). Prec = 6 # setting accuracy, i.e. the maximum number of decimal places

 

>>>Decimal('5')/3

Decimal('1.66667')

>>>getcontext().prec = 2

>>>Decimal('5')/3

Decimal('1.7')

>>>Decimal('5')/Decimal(3)

Decimal('1.7')

 

>>>Decimal('3.14')

Decimal('3.14')

 

>>>Decimal('3.14') * Decimal(0.5)

Decimal('1.570')

 

>>>Decimal('3.1415926535')

Decimal('3.1415926535')

 

>>>Decimal('NaN')

Decimal ( 'in')

 

>>>Decimal('-Infinity')

Decimal('-Infinity')

 

Conclusion: if and only if the accuracy of "calculation result" exceeds the number of decimal places getContext () set at the time, it will exceed the accuracy of decimal places truncated portion

Guess you like

Origin www.cnblogs.com/shouke/p/11776300.html