New submission from Daniel Lescohier <daniel.lescoh...@cbs.com>: 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 ---------- components: Library (Lib) message_count: 1.0 messages: 83328 nosy: dlesco nosy_count: 1.0 severity: normal status: open title: Add precision property to decimal.Decimal versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue5448> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com