[Python-checkins] r60254 - in python/trunk/Lib: decimal.py test/test_decimal.py

raymond.hettinger python-checkins at python.org
Thu Jan 24 20:05:29 CET 2008


Author: raymond.hettinger
Date: Thu Jan 24 20:05:29 2008
New Revision: 60254

Modified:
   python/trunk/Lib/decimal.py
   python/trunk/Lib/test/test_decimal.py
Log:
Add support for trunc().

Modified: python/trunk/Lib/decimal.py
==============================================================================
--- python/trunk/Lib/decimal.py	(original)
+++ python/trunk/Lib/decimal.py	Thu Jan 24 20:05:29 2008
@@ -1433,6 +1433,8 @@
         else:
             return s*int(self._int[:self._exp] or '0')
 
+    __trunc__ = __int__
+
     def __long__(self):
         """Converts to a long.
 

Modified: python/trunk/Lib/test/test_decimal.py
==============================================================================
--- python/trunk/Lib/test/test_decimal.py	(original)
+++ python/trunk/Lib/test/test_decimal.py	Thu Jan 24 20:05:29 2008
@@ -1151,6 +1151,7 @@
         checkSameDec("__floordiv__", True)
         checkSameDec("__hash__")
         checkSameDec("__int__")
+        checkSameDec("__trunc__")
         checkSameDec("__long__")
         checkSameDec("__mod__", True)
         checkSameDec("__mul__", True)
@@ -1216,6 +1217,16 @@
             r = d.to_integral(ROUND_DOWN)
             self.assertEqual(Decimal(int(d)), r)
 
+    def test_trunc(self):
+        for x in range(-250, 250):
+            s = '%0.2f' % (x / 100.0)
+            # should work the same as for floats
+            self.assertEqual(int(Decimal(s)), int(float(s)))
+            # should work the same as to_integral in the ROUND_DOWN mode
+            d = Decimal(s)
+            r = d.to_integral(ROUND_DOWN)
+            self.assertEqual(Decimal(trunc(d)), r)
+
 class ContextAPItests(unittest.TestCase):
 
     def test_pickle(self):


More information about the Python-checkins mailing list