]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
parse_decimal() now returns Decimals not floats, API change (#178)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 6 Aug 2012 20:19:20 +0000 (20:19 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Mon, 6 Aug 2012 20:19:20 +0000 (20:19 +0000)
ChangeLog
babel/numbers.py
babel/tests/numbers.py

index d9bf07ade92c0b5cbe413be81858e61ef2dd663d..808e41d65999f3e3478f15dfa24118eca45ee03a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -45,6 +45,7 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
  * fix format_decimal() with small Decimal values (#214, patch from George Lund)
  * fix handling of messages containing '\\n' (#198)
  * handle irregular multi-line msgstr (no "" as first line) gracefully (#171)
+ * parse_decimal() now returns Decimals not floats, API change (#178)
 
 
 Version 0.9.6
index 8b151614460e256619bee50c88acfe94d3312447..0f4fe0f48bd3f603cdb6fe6bf67204e3b792089a 100644 (file)
@@ -23,7 +23,7 @@ following environment variables, in that order:
 # TODO:
 #  Padding and rounding increments in pattern:
 #  - http://www.unicode.org/reports/tr35/ (Appendix G.6)
-from decimal import Decimal
+from decimal import Decimal, InvalidOperation
 import math
 import re
 
@@ -281,12 +281,12 @@ def parse_number(string, locale=LC_NUMERIC):
         raise NumberFormatError('%r is not a valid number' % string)
 
 def parse_decimal(string, locale=LC_NUMERIC):
-    """Parse localized decimal string into a float.
+    """Parse localized decimal string into a decimal.
     
     >>> parse_decimal('1,099.98', locale='en_US')
-    1099.98
+    Decimal('1099.98')
     >>> parse_decimal('1.099,98', locale='de')
-    1099.98
+    Decimal('1099.98')
     
     When the given string cannot be parsed, an exception is raised:
     
@@ -298,15 +298,15 @@ def parse_decimal(string, locale=LC_NUMERIC):
     :param string: the string to parse
     :param locale: the `Locale` object or locale identifier
     :return: the parsed decimal number
-    :rtype: `float`
+    :rtype: `Decimal`
     :raise `NumberFormatError`: if the string can not be converted to a
                                 decimal number
     """
     locale = Locale.parse(locale)
     try:
-        return float(string.replace(get_group_symbol(locale), '')
+        return Decimal(string.replace(get_group_symbol(locale), '')
                            .replace(get_decimal_symbol(locale), '.'))
-    except ValueError:
+    except InvalidOperation:
         raise NumberFormatError('%r is not a valid decimal number' % string)
 
 
index 6a19ee6077be66f2084257ea5662c8beda163b4c..ccc76b05db7b14fd5bb7fd0ff3d085c3c6cc7ebd 100644 (file)
@@ -162,11 +162,22 @@ class BankersRoundTestCase(unittest.TestCase):
         self.assertEqual(Decimal('0.2'), numbers.bankersround(Decimal('0.15'), ndigits=1))
 
 
+class NumberParsingTestCase(unittest.TestCase):
+    def test_can_parse_decimals(self):
+        self.assertEqual(Decimal('1099.98'), 
+            numbers.parse_decimal('1,099.98', locale='en_US'))
+        self.assertEqual(Decimal('1099.98'), 
+            numbers.parse_decimal('1.099,98', locale='de'))
+        self.assertRaises(numbers.NumberFormatError, 
+                          lambda: numbers.parse_decimal('2,109,998', locale='de'))
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(numbers))
     suite.addTest(unittest.makeSuite(FormatDecimalTestCase))
     suite.addTest(unittest.makeSuite(BankersRoundTestCase))
+    suite.addTest(unittest.makeSuite(NumberParsingTestCase))
     return suite
 
 if __name__ == '__main__':