2014-11-30 16:29:52 +08:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2010-09-27 23:25:08 +08:00
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2011-12-24 19:01:57 +08:00
|
|
|
def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
|
|
|
|
force_grouping=False):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
Gets a number (as a number or string), and returns it as a string,
|
2011-12-24 19:01:57 +08:00
|
|
|
using formats defined as arguments:
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
* decimal_sep: Decimal separator symbol (for example ".")
|
|
|
|
* decimal_pos: Number of decimal positions
|
2016-06-03 03:13:47 +08:00
|
|
|
* grouping: Number of digits in every group limited by thousand separator.
|
|
|
|
For non-uniform digit grouping, it can be a sequence with the number
|
|
|
|
of digit group sizes following the format used by the Python locale
|
|
|
|
module in locale.localeconv() LC_NUMERIC grouping (e.g. (3, 2, 0)).
|
2009-12-23 01:58:49 +08:00
|
|
|
* thousand_sep: Thousand separator symbol (for example ",")
|
|
|
|
"""
|
2011-12-24 19:01:57 +08:00
|
|
|
use_grouping = settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR
|
|
|
|
use_grouping = use_grouping or force_grouping
|
2016-06-03 03:13:47 +08:00
|
|
|
use_grouping = use_grouping and grouping != 0
|
2011-12-24 19:01:57 +08:00
|
|
|
# Make the common case fast
|
2010-09-27 23:25:08 +08:00
|
|
|
if isinstance(number, int) and not use_grouping and not decimal_pos:
|
2016-12-29 23:27:49 +08:00
|
|
|
return mark_safe(str(number))
|
2009-12-23 01:58:49 +08:00
|
|
|
# sign
|
2012-09-09 00:29:29 +08:00
|
|
|
sign = ''
|
2014-11-30 16:29:52 +08:00
|
|
|
if isinstance(number, Decimal):
|
|
|
|
str_number = '{:f}'.format(number)
|
|
|
|
else:
|
2016-12-29 23:27:49 +08:00
|
|
|
str_number = str(number)
|
2009-12-23 01:58:49 +08:00
|
|
|
if str_number[0] == '-':
|
2012-09-09 00:29:29 +08:00
|
|
|
sign = '-'
|
2009-12-23 01:58:49 +08:00
|
|
|
str_number = str_number[1:]
|
2010-09-27 23:25:08 +08:00
|
|
|
# decimal part
|
2009-12-23 01:58:49 +08:00
|
|
|
if '.' in str_number:
|
|
|
|
int_part, dec_part = str_number.split('.')
|
2011-04-22 20:03:10 +08:00
|
|
|
if decimal_pos is not None:
|
2009-12-23 01:58:49 +08:00
|
|
|
dec_part = dec_part[:decimal_pos]
|
|
|
|
else:
|
|
|
|
int_part, dec_part = str_number, ''
|
2011-04-22 20:03:10 +08:00
|
|
|
if decimal_pos is not None:
|
2009-12-23 01:58:49 +08:00
|
|
|
dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
|
2011-12-24 19:01:57 +08:00
|
|
|
if dec_part:
|
|
|
|
dec_part = decimal_sep + dec_part
|
2009-12-23 01:58:49 +08:00
|
|
|
# grouping
|
2010-09-27 23:25:08 +08:00
|
|
|
if use_grouping:
|
2016-06-03 03:13:47 +08:00
|
|
|
try:
|
|
|
|
# if grouping is a sequence
|
|
|
|
intervals = list(grouping)
|
|
|
|
except TypeError:
|
|
|
|
# grouping is a single value
|
|
|
|
intervals = [grouping, 0]
|
|
|
|
active_interval = intervals.pop(0)
|
2009-12-23 01:58:49 +08:00
|
|
|
int_part_gd = ''
|
2016-06-03 03:13:47 +08:00
|
|
|
cnt = 0
|
|
|
|
for digit in int_part[::-1]:
|
|
|
|
if cnt and cnt == active_interval:
|
|
|
|
if intervals:
|
|
|
|
active_interval = intervals.pop(0) or active_interval
|
2015-02-08 05:36:45 +08:00
|
|
|
int_part_gd += thousand_sep[::-1]
|
2016-06-03 03:13:47 +08:00
|
|
|
cnt = 0
|
2009-12-23 01:58:49 +08:00
|
|
|
int_part_gd += digit
|
2016-06-03 03:13:47 +08:00
|
|
|
cnt += 1
|
2009-12-23 01:58:49 +08:00
|
|
|
int_part = int_part_gd[::-1]
|
|
|
|
return sign + int_part + dec_part
|