From: Felix Schwarz Date: Sat, 28 Jul 2012 22:26:02 +0000 (+0000) Subject: fix formatting of fraction in format_decimal() if the input value is a float with... X-Git-Tag: 1.0~171 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=858d8f70e34200ad759a77c2af8c43cbadc8d51e;p=thirdparty%2Fbabel.git 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 index 05e1127b..14722b2d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -39,6 +39,8 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/ * 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 index 632dd640..795a0fa2 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -478,6 +478,8 @@ class NumberPattern(object): 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 index f4d1d4e3..caaa1d08 100644 --- a/babel/tests/numbers.py +++ b/babel/tests/numbers.py @@ -28,6 +28,12 @@ class FormatDecimalTestCase(unittest.TestCase): 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.##;-#',