Message83328
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 |
|
Date |
User |
Action |
Args |
2009-03-08 20:23:46 | dlesco | set | recipients:
+ dlesco |
2009-03-08 20:23:45 | dlesco | set | messageid: <1236543825.99.0.833291294426.issue5448@psf.upfronthosting.co.za> |
2009-03-08 20:23:44 | dlesco | link | issue5448 messages |
2009-03-08 20:23:43 | dlesco | create | |
|