Fixed #17414 -- Prevented numberformat from trying to group digits when the number of digits per group is zero.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17267 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-12-24 11:01:57 +00:00
parent 1c16907199
commit ddb2a9eb9e
2 changed files with 15 additions and 7 deletions

View File

@ -53,6 +53,12 @@ class HumanizeTests(TestCase):
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):
self.humanize_tester(test_list, result_list, 'intcomma') self.humanize_tester(test_list, result_list, 'intcomma')
def test_intcomma_without_number_grouping(self):
# Regression for #17414
with translation.override('ja'):
with self.settings(USE_L10N=True):
self.humanize_tester([100], ['100'], 'intcomma')
def test_intword(self): def test_intword(self):
test_list = ('100', '1000000', '1200000', '1290000', test_list = ('100', '1000000', '1200000', '1290000',
'1000000000', '2000000000', '6000000000000', '1000000000', '2000000000', '6000000000000',

View File

@ -2,20 +2,21 @@ from django.conf import settings
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', force_grouping=False): def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
force_grouping=False):
""" """
Gets a number (as a number or string), and returns it as a string, Gets a number (as a number or string), and returns it as a string,
using formats definied as arguments: using formats defined as arguments:
* decimal_sep: Decimal separator symbol (for example ".") * decimal_sep: Decimal separator symbol (for example ".")
* decimal_pos: Number of decimal positions * decimal_pos: Number of decimal positions
* grouping: Number of digits in every group limited by thousand separator * grouping: Number of digits in every group limited by thousand separator
* thousand_sep: Thousand separator symbol (for example ",") * thousand_sep: Thousand separator symbol (for example ",")
""" """
use_grouping = force_grouping or settings.USE_L10N and \ use_grouping = settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR
settings.USE_THOUSAND_SEPARATOR and grouping use_grouping = use_grouping or force_grouping
# Make the common case fast: use_grouping = use_grouping and grouping > 0
# Make the common case fast
if isinstance(number, int) and not use_grouping and not decimal_pos: if isinstance(number, int) and not use_grouping and not decimal_pos:
return mark_safe(unicode(number)) return mark_safe(unicode(number))
# sign # sign
@ -35,7 +36,8 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', f
int_part, dec_part = str_number, '' int_part, dec_part = str_number, ''
if decimal_pos is not None: if decimal_pos is not None:
dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
if dec_part: dec_part = decimal_sep + dec_part if dec_part:
dec_part = decimal_sep + dec_part
# grouping # grouping
if use_grouping: if use_grouping:
int_part_gd = '' int_part_gd = ''