Significant figures calculation

steve+comp.lang.python at pearwood.info steve+comp.lang.python at pearwood.info
Fri Jun 24 20:31:50 EDT 2011


Jerry Hill wrote:

> I'm curious.  Is there a way to get the number of significant digits
> for a particular Decimal instance?  I spent a few minutes browsing
> through the docs, and didn't see anything obvious.  I was thinking
> about setting the precision dynamically within a function, based on
> the significance of the inputs.

Not officially, so far as I know, but if you're willing to risk using a
private implementation detail that is subject to change:

>>> decimal.Decimal('000123.45000')._int
'12345000'

However, sometimes you may need to inspect the exponent as well:

>>> zero = decimal.Decimal('0.00000000')
>>> zero._int
'0'
>>> zero._exp
-8



-- 
Steven




More information about the Python-list mailing list