[Python-checkins] r60794 - python/trunk/Doc/library/decimal.rst
raymond.hettinger
python-checkins at python.org
Thu Feb 14 12:57:26 CET 2008
Author: raymond.hettinger
Date: Thu Feb 14 12:57:25 2008
New Revision: 60794
Modified:
python/trunk/Doc/library/decimal.rst
Log:
Show how to remove exponents.
Modified: python/trunk/Doc/library/decimal.rst
==============================================================================
--- python/trunk/Doc/library/decimal.rst (original)
+++ python/trunk/Doc/library/decimal.rst Thu Feb 14 12:57:25 2008
@@ -1590,6 +1590,7 @@
... 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 @@
: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
More information about the Python-checkins
mailing list