diff options
| author | Jannis Leidel <jannis@leidel.info> | 2009-12-22 17:58:49 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2009-12-22 17:58:49 +0000 |
| commit | 9233d0426537615e06b78d28010d17d5a66adf44 (patch) | |
| tree | 5c731977b3ef9bd1e660b63c4edf6caa7b6491ad /django/utils/numberformat.py | |
| parent | 6632739e94c6c38b4c5a86cf5c80c48ae50ac49f (diff) | |
Fixed #7980 - Improved i18n framework to support locale aware formatting (dates and numbers) and form processing.
Thanks to Marc Garcia for working on this during his Google Summer of Code 2009!
Additionally fixes #1061, #2203, #3940, #5526, #6449, #6231, #6693, #6783, #9366 and #10891.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/numberformat.py')
| -rw-r--r-- | django/utils/numberformat.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py new file mode 100644 index 0000000000..78ecb2fbdb --- /dev/null +++ b/django/utils/numberformat.py @@ -0,0 +1,42 @@ +from django.conf import settings + +def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''): + """ + Gets a number (as a number or string), and returns it as a string, + using formats definied as arguments: + + * decimal_sep: Decimal separator symbol (for example ".") + * decimal_pos: Number of decimal positions + * grouping: Number of digits in every group limited by thousand separator + * thousand_sep: Thousand separator symbol (for example ",") + + """ + # sign + if float(number) < 0: + sign = '-' + else: + sign = '' + # decimal part + str_number = unicode(number) + if str_number[0] == '-': + str_number = str_number[1:] + if '.' in str_number: + int_part, dec_part = str_number.split('.') + if decimal_pos: + dec_part = dec_part[:decimal_pos] + else: + int_part, dec_part = str_number, '' + if decimal_pos: + dec_part = dec_part + ('0' * (decimal_pos - len(dec_part))) + if dec_part: dec_part = decimal_sep + dec_part + # grouping + if settings.USE_THOUSAND_SEPARATOR and 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 + |
