summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-14 11:57:25 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-14 11:57:25 (GMT)
commitd68bf02810b76db382004acb2f60a0515381feec (patch)
tree3ac01802e64e11f7449774efd8667ce498b820e0 /Doc
parent27a90d989db3b6e748ccf08556464a6b830c19f0 (diff)
downloadcpython-d68bf02810b76db382004acb2f60a0515381feec.zip
cpython-d68bf02810b76db382004acb2f60a0515381feec.tar.gz
cpython-d68bf02810b76db382004acb2f60a0515381feec.tar.bz2
Show how to remove exponents.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/decimal.rst11
1 files changed, 11 insertions, 0 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst
index 2f191bd..0a8262d 100644
--- a/Doc/library/decimal.rst
+++ b/Doc/library/decimal.rst
@@ -1590,6 +1590,7 @@ to handle the :meth:`quantize` step::
... return (x * y).quantize(fp)
>>> def div(x, y, fp=TWOPLACES):
... return (x / y).quantize(fp)
+
>>> mul(a, b) # Automatically preserve fixed-point
Decimal('325.62')
>>> div(b, a)
@@ -1615,6 +1616,16 @@ of significant places in the coefficient. For example, expressing
:const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the
original's two-place significance.
+If an application does not care about tracking significance, it is easy to
+remove the exponent and trailing zeroes, losing signficance, but keeping the
+value unchanged::
+
+ >>> def remove_exponent(d):
+ ... return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
+
+ >>> remove_exponent(Decimal('5E+3'))
+ Decimal('5000')
+
Q. Is there a way to convert a regular float to a :class:`Decimal`?
A. Yes, all binary floating point numbers can be exactly expressed as a