]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
include more information in error message
authorChangaco <changaco@changaco.oy.lc>
Sun, 17 Jun 2018 07:17:02 +0000 (09:17 +0200)
committerChangaco <changaco@changaco.oy.lc>
Sun, 17 Jun 2018 08:46:34 +0000 (10:46 +0200)
babel/numbers.py
tests/test_numbers.py

index 509150cb9cb230f4ab456c66a3e9993794457a36..b0cfe65e7389789b95cd1e95c4234841543251ff 100644 (file)
@@ -682,7 +682,7 @@ def parse_decimal(string, locale=LC_NUMERIC, strict=False):
     >>> parse_decimal('30.00', locale='de', strict=True)
     Traceback (most recent call last):
         ...
-    NumberFormatError: '30.00' is not a properly formatted decimal number
+    NumberFormatError: '30.00' is not a properly formatted decimal number. Did you mean '3.000'? Or maybe '30,00'?
 
     :param string: the string to parse
     :param locale: the `Locale` object or locale identifier
@@ -702,9 +702,20 @@ def parse_decimal(string, locale=LC_NUMERIC, strict=False):
     if strict and group_symbol in string:
         proper = format_decimal(parsed, locale=locale, decimal_quantization=False)
         if string != proper and string.rstrip('0') != (proper + decimal_symbol):
-            raise NumberFormatError(
-                "%r is not a properly formatted decimal number" % string
-            )
+            try:
+                parsed_alt = decimal.Decimal(string.replace(decimal_symbol, '')
+                                                   .replace(group_symbol, '.'))
+            except decimal.InvalidOperation:
+                raise NumberFormatError(
+                    "%r is not a properly formatted decimal number. Did you mean %r?" %
+                    (string, proper)
+                )
+            else:
+                proper_alt = format_decimal(parsed_alt, locale=locale, decimal_quantization=False)
+                raise NumberFormatError(
+                    "%r is not a properly formatted decimal number. Did you mean %r? Or maybe %r?" %
+                    (string, proper, proper_alt)
+                )
     return parsed
 
 
index 50c53dec4470308d96ca8ad66d3584a457821520..b9dcb72783344fed997ff3af3567069f96503e79 100644 (file)
@@ -169,6 +169,9 @@ class NumberParsingTestCase(unittest.TestCase):
         # Numbers with a misplaced grouping symbol should be rejected
         with self.assertRaises(numbers.NumberFormatError):
             numbers.parse_decimal('11.11', locale='de', strict=True)
+        # Numbers with two misplaced grouping symbols should be rejected
+        with self.assertRaises(numbers.NumberFormatError):
+            numbers.parse_decimal('80.00.00', locale='de', strict=True)
         # Partially grouped numbers should be rejected
         with self.assertRaises(numbers.NumberFormatError):
             numbers.parse_decimal('2000,000', locale='en_US', strict=True)