This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dlesco
Recipients dlesco
Date 2009-03-08.20:23:43
SpamBayes Score 4.1528918e-05
Marked as misclassified No
Message-id <1236543825.99.0.833291294426.issue5448@psf.upfronthosting.co.za>
In-reply-to
Content
I would like to get the decimal precision of decimal.Decimal objects 
(for my application, in order to validate whether a Decimal value will 
fit in the defined decimal precision of a database field). The way I 
found to get it was with:

precision = len(value._int)

where value is a decimal.Decimal object.

However, this uses a private API (_int). I would like to have a public 
API to get the decimal precision of a Decimal.  I propose we add the 
following to the decimal.Decimal class:

    @property
    def precision(self):
        """The precision of this Decimal value."""
        return len(self._int)

decimal.Context has a precision for calculations. 
decimal.Decimal.precision is the minimum precision needed to represent 
that value, not the precision that was used in calculating it.  If one 
wants to, one can actually use Decimal.precision to set your Context's 
precision:

d1 = decimal.Decimal('999')
d2 = d1
context.prec = d1.precision + d2.precision
d3 = d1 * d2

Open for debate is whether to name it Decimal.prec to mirror 
Context.prec.  We'd have to choose one or the other or both:

    @property
    def precision(self):
        """The precision of this Decimal value."""
        return len(self._int)
    prec = precision
History
Date User Action Args
2009-03-08 20:23:46dlescosetrecipients: + dlesco
2009-03-08 20:23:45dlescosetmessageid: <1236543825.99.0.833291294426.issue5448@psf.upfronthosting.co.za>
2009-03-08 20:23:44dlescolinkissue5448 messages
2009-03-08 20:23:43dlescocreate