Python Forum

Full Version: Trouble with decimal precision
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, I have a problem with precision in decimal, I want got 10 numbers after coma, but with context 10 I got 15, why? Thanks.
with localcontext() as ctx:
... ctx.prec = 10
... print(Decimal('1.0000000000') * Decimal('0.0000042372'))
...
0.000004237200000
>>> with localcontext() as ctx:
... ctx.prec = 5
... print(Decimal('1.0000000000') * Decimal('0.0000042372'))
I think it's working as intended. "precision" is ignoring the leading zeros, instead of being how many decimal places to include. Check this out:
>>> import decimal as dec
>>> dec.getcontext().prec = 1
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004
>>> dec.getcontext().prec = 5
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.0000042372
>>> dec.getcontext().prec = 10
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004237200000
>>> with dec.localcontext() as ctx:
...   ctx.prec = 1
...   print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
...
0.000004
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004237200000
I think what you're looking for isn't the context, but rather the quantize method: https://docs.python.org/3.7/library/deci...l.quantize

Quote:
>>> Decimal('1.41421356').quantize(Decimal('1.000'))
Decimal('1.414')

At the bottom of the docs, in the FAQ, is this snippet:
Quote:
>>> TWOPLACES = Decimal(10) ** -2       # same as Decimal('0.01')
>>> # Round to two places
>>> Decimal('3.214').quantize(TWOPLACES)
Decimal('3.21')
You use setcontext, getcontext, and precision, see the example under the "Precision" heading at https://pymotw.com/3/decimal/
(Feb-14-2019, 08:14 PM)nilamo Wrote: [ -> ]I think it's working as intended. "precision" is ignoring the leading zeros, instead of being how many decimal places to include. Check this out:
>>> import decimal as dec
>>> dec.getcontext().prec = 1
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004
>>> dec.getcontext().prec = 5
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.0000042372
>>> dec.getcontext().prec = 10
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004237200000
>>> with dec.localcontext() as ctx:
...   ctx.prec = 1
...   print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
...
0.000004
>>> print(dec.Decimal('1.00000000000000') * dec.Decimal('0.0000042372'))
0.000004237200000

Thanks! I didn't think's about this.