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='',
|
2018-07-20 04:44:40 +08:00
|
|
|
force_grouping=False, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Get a number (as a number or string), and return 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 ",")
|
|
|
|
"""
|
2018-07-20 04:44:40 +08:00
|
|
|
use_grouping = (use_l10n or (use_l10n is None and settings.USE_L10N)) and settings.USE_THOUSAND_SEPARATOR
|
2011-12-24 19:01:57 +08:00
|
|
|
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:
|
2018-07-09 23:25:11 +08:00
|
|
|
return mark_safe(number)
|
2009-12-23 01:58:49 +08:00
|
|
|
# sign
|
2012-09-09 00:29:29 +08:00
|
|
|
sign = ''
|
2020-02-23 21:37:02 +08:00
|
|
|
# Treat potentially very large/small floats as Decimals.
|
|
|
|
if isinstance(number, float) and 'e' in str(number).lower():
|
|
|
|
number = Decimal(str(number))
|
2014-11-30 16:29:52 +08:00
|
|
|
if isinstance(number, Decimal):
|
2019-04-13 18:11:34 +08:00
|
|
|
|
|
|
|
if decimal_pos is not None:
|
|
|
|
# If the provided number is too small to affect any of the visible
|
|
|
|
# decimal places, consider it equal to '0'.
|
|
|
|
cutoff = Decimal('0.' + '1'.rjust(decimal_pos, '0'))
|
|
|
|
if abs(number) < cutoff:
|
|
|
|
number = Decimal('0')
|
|
|
|
|
2019-02-11 18:08:45 +08:00
|
|
|
# Format values with more than 200 digits (an arbitrary cutoff) using
|
|
|
|
# scientific notation to avoid high memory usage in {:f}'.format().
|
|
|
|
_, digits, exponent = number.as_tuple()
|
|
|
|
if abs(exponent) + len(digits) > 200:
|
|
|
|
number = '{:e}'.format(number)
|
|
|
|
coefficient, exponent = number.split('e')
|
|
|
|
# Format the coefficient.
|
|
|
|
coefficient = format(
|
|
|
|
coefficient, decimal_sep, decimal_pos, grouping,
|
|
|
|
thousand_sep, force_grouping, use_l10n,
|
|
|
|
)
|
|
|
|
return '{}e{}'.format(coefficient, exponent)
|
|
|
|
else:
|
|
|
|
str_number = '{:f}'.format(number)
|
2014-11-30 16:29:52 +08:00
|
|
|
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)))
|
2018-01-04 07:52:12 +08:00
|
|
|
dec_part = dec_part and 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
|