From 01d0bf011ef9c92892fa0717764d74ecfd2ef2b6 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Apr 2011 12:03:10 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#13810=20--=20Truncate=20numbers=20corr?= =?UTF-8?q?ectly=20when=20given=20number=20of=20decimal=20positions=20is?= =?UTF-8?q?=20zero.=20Thanks,=20milosu=20=20and=20=C5=81ukasz=20Rekucki.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@16074 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/numberformat.py | 6 +++--- tests/regressiontests/i18n/tests.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py index 069f49851b..9597cdd063 100644 --- a/django/utils/numberformat.py +++ b/django/utils/numberformat.py @@ -2,7 +2,7 @@ from django.conf import settings from django.utils.safestring import mark_safe -def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): +def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep=''): """ Gets a number (as a number or string), and returns it as a string, using formats definied as arguments: @@ -29,11 +29,11 @@ def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): # decimal part if '.' in str_number: int_part, dec_part = str_number.split('.') - if decimal_pos: + if decimal_pos is not None: dec_part = dec_part[:decimal_pos] else: int_part, dec_part = str_number, '' - if decimal_pos: + if decimal_pos is not None: dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) if dec_part: dec_part = decimal_sep + dec_part # grouping diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index c92823b9c7..2a7f0c2841 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -171,6 +171,7 @@ class FormattingTests(TestCase): settings.USE_THOUSAND_SEPARATOR = False self.assertEqual(u'66666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=',')) self.assertEqual(u'66666A6', nformat(self.n, decimal_sep='A', decimal_pos=1, grouping=1, thousand_sep='B')) + self.assertEqual(u'66666', nformat(self.n, decimal_sep='X', decimal_pos=0, grouping=1, thousand_sep='Y')) settings.USE_THOUSAND_SEPARATOR = True self.assertEqual(u'66,666.66', nformat(self.n, decimal_sep='.', decimal_pos=2, grouping=3, thousand_sep=','))