diff options
| author | Jannis Leidel <jannis@leidel.info> | 2010-09-27 15:25:08 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2010-09-27 15:25:08 +0000 |
| commit | 534792d05591eb2d19dc48ee89dcd2d9b5d461c0 (patch) | |
| tree | f5e2fcd19541726fe51fc7c9507300733053b1da /django/utils/numberformat.py | |
| parent | 9c402f0ecc3751a746cc0aeaa24b7b8320af70f7 (diff) | |
Fixed #14290 -- Made format localization faster by caching the format modules. Thanks, Teemu Kurppa and Anssi Kääriäinen for the report and initial patches.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13898 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/numberformat.py')
| -rw-r--r-- | django/utils/numberformat.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py index 129c27f4da..069f49851b 100644 --- a/django/utils/numberformat.py +++ b/django/utils/numberformat.py @@ -1,4 +1,6 @@ from django.conf import settings +from django.utils.safestring import mark_safe + def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): """ @@ -11,15 +13,20 @@ def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): * thousand_sep: Thousand separator symbol (for example ",") """ + use_grouping = settings.USE_L10N and \ + settings.USE_THOUSAND_SEPARATOR and grouping + # Make the common case fast: + if isinstance(number, int) and not use_grouping and not decimal_pos: + return mark_safe(unicode(number)) # sign if float(number) < 0: sign = '-' else: sign = '' - # decimal part str_number = unicode(number) if str_number[0] == '-': str_number = str_number[1:] + # decimal part if '.' in str_number: int_part, dec_part = str_number.split('.') if decimal_pos: @@ -30,13 +37,12 @@ def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) if dec_part: dec_part = decimal_sep + dec_part # grouping - if settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR and grouping: + if use_grouping: int_part_gd = '' for cnt, digit in enumerate(int_part[::-1]): if cnt and not cnt % grouping: int_part_gd += thousand_sep int_part_gd += digit int_part = int_part_gd[::-1] - return sign + int_part + dec_part |
