# HG changeset patch # User fschwarz # Date 1343514362 0 # Node ID e77dd06c40ef431dce6133ad8e02e5781a7291b9 # Parent 8ce41e60f90d9c64f5f1e7508225a1da7bd01894 fix formatting of fraction in format_decimal() if the input value is a float with more than 7 significant digits (#183) diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -39,6 +39,8 @@ * Support for context-aware methods during message extraction (#229, patch from David Rios) * "init" and "update" commands support "--no-wrap" option (#289) + * fix formatting of fraction in format_decimal() if the input value is a float + with more than 7 significant digits (#183) Version 0.9.6 http://svn.edgewall.org/repos/babel/tags/0.9.6/ diff --git a/babel/numbers.py b/babel/numbers.py --- a/babel/numbers.py +++ b/babel/numbers.py @@ -478,6 +478,8 @@ return '<%s %r>' % (type(self).__name__, self.pattern) def apply(self, value, locale, currency=None): + if isinstance(value, float): + value = Decimal(str(value)) value *= self.scale is_negative = int(value < 0) if self.exp_prec: # Scientific notation diff --git a/babel/tests/numbers.py b/babel/tests/numbers.py --- a/babel/tests/numbers.py +++ b/babel/tests/numbers.py @@ -28,6 +28,12 @@ self.assertEqual(numbers.format_decimal(10.0**20, '#.00', locale='en_US'), '100000000000000000000.00') + # regression test for #183, fraction digits were not correctly cutted + # if the input was a float value and the value had more than 7 + # significant digits + self.assertEqual(u'12,345,678.05', + numbers.format_decimal(12345678.051, '#,##0.00', + locale='en_US')) def test_subpatterns(self): self.assertEqual(numbers.format_decimal(-12345, '#,##0.##;-#',