I want to limit the precision of my float and use it as an exact value for next operations, but I'm doing something wrong with decimal module.
from decimal import *
factor = 350/float(255)
# Settings
getcontext().prec = 1
getcontext().rounding = ROUND_UP
print Decimal(factor)
Python output: 1.372549019607843145962533526471816003322601318359375
Expected and wanted result: 1.4
From Python Documentation: ( https://docs.python.org/2/library/decimal.html )
The significance of a new Decimal is determined solely by the number of digits input. Context precision and rounding only come into play during arithmetic operations .
getcontext().prec = 6
Decimal('3.0')
Decimal('3.0')
Decimal('3.1415926535')
Decimal('3.1415926535')
Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987')
getcontext().rounding = ROUND_UP
Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85988')
so you need to use decimal in operation, not when printing result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.