diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/utils/numberformat.py | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/utils/numberformat.py')
| -rw-r--r-- | django/utils/numberformat.py | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py index 3bfdb2ea52..488d6a77cd 100644 --- a/django/utils/numberformat.py +++ b/django/utils/numberformat.py @@ -4,8 +4,15 @@ from django.conf import settings from django.utils.safestring import mark_safe -def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', - force_grouping=False, use_l10n=None): +def format( + number, + decimal_sep, + decimal_pos=None, + grouping=0, + thousand_sep="", + force_grouping=False, + use_l10n=None, +): """ Get a number (as a number or string), and return it as a string, using formats defined as arguments: @@ -18,54 +25,61 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', module in locale.localeconv() LC_NUMERIC grouping (e.g. (3, 2, 0)). * thousand_sep: Thousand separator symbol (for example ",") """ - use_grouping = (use_l10n or (use_l10n is None and settings.USE_L10N)) and settings.USE_THOUSAND_SEPARATOR + use_grouping = ( + use_l10n or (use_l10n is None and settings.USE_L10N) + ) and settings.USE_THOUSAND_SEPARATOR use_grouping = use_grouping or force_grouping use_grouping = use_grouping and grouping != 0 # Make the common case fast if isinstance(number, int) and not use_grouping and not decimal_pos: return mark_safe(number) # sign - sign = '' + sign = "" # Treat potentially very large/small floats as Decimals. - if isinstance(number, float) and 'e' in str(number).lower(): + if isinstance(number, float) and "e" in str(number).lower(): number = Decimal(str(number)) if isinstance(number, Decimal): 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')) + cutoff = Decimal("0." + "1".rjust(decimal_pos, "0")) if abs(number) < cutoff: - number = Decimal('0') + number = Decimal("0") # 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') + 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, + coefficient, + decimal_sep, + decimal_pos, + grouping, + thousand_sep, + force_grouping, + use_l10n, ) - return '{}e{}'.format(coefficient, exponent) + return "{}e{}".format(coefficient, exponent) else: - str_number = '{:f}'.format(number) + str_number = "{:f}".format(number) else: str_number = str(number) - if str_number[0] == '-': - sign = '-' + if str_number[0] == "-": + sign = "-" str_number = str_number[1:] # decimal part - if '.' in str_number: - int_part, dec_part = str_number.split('.') + if "." in str_number: + int_part, dec_part = str_number.split(".") if decimal_pos is not None: dec_part = dec_part[:decimal_pos] else: - int_part, dec_part = str_number, '' + int_part, dec_part = str_number, "" 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))) dec_part = dec_part and decimal_sep + dec_part # grouping if use_grouping: @@ -76,7 +90,7 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='', # grouping is a single value intervals = [grouping, 0] active_interval = intervals.pop(0) - int_part_gd = '' + int_part_gd = "" cnt = 0 for digit in int_part[::-1]: if cnt and cnt == active_interval: |
