You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

numberformat.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from decimal import Decimal
  2. from django.conf import settings
  3. from django.utils.safestring import mark_safe
  4. def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
  5. force_grouping=False, use_l10n=None):
  6. """
  7. Get a number (as a number or string), and return it as a string,
  8. using formats defined as arguments:
  9. * decimal_sep: Decimal separator symbol (for example ".")
  10. * decimal_pos: Number of decimal positions
  11. * grouping: Number of digits in every group limited by thousand separator.
  12. For non-uniform digit grouping, it can be a sequence with the number
  13. of digit group sizes following the format used by the Python locale
  14. module in locale.localeconv() LC_NUMERIC grouping (e.g. (3, 2, 0)).
  15. * thousand_sep: Thousand separator symbol (for example ",")
  16. """
  17. use_grouping = (use_l10n or (use_l10n is None and settings.USE_L10N)) and settings.USE_THOUSAND_SEPARATOR
  18. use_grouping = use_grouping or force_grouping
  19. use_grouping = use_grouping and grouping != 0
  20. # Make the common case fast
  21. if isinstance(number, int) and not use_grouping and not decimal_pos:
  22. return mark_safe(number)
  23. # sign
  24. sign = ''
  25. if isinstance(number, Decimal):
  26. # Format values with more than 200 digits (an arbitrary cutoff) using
  27. # scientific notation to avoid high memory usage in {:f}'.format().
  28. _, digits, exponent = number.as_tuple()
  29. if abs(exponent) + len(digits) > 200:
  30. number = '{:e}'.format(number)
  31. coefficient, exponent = number.split('e')
  32. # Format the coefficient.
  33. coefficient = format(
  34. coefficient, decimal_sep, decimal_pos, grouping,
  35. thousand_sep, force_grouping, use_l10n,
  36. )
  37. return '{}e{}'.format(coefficient, exponent)
  38. else:
  39. str_number = '{:f}'.format(number)
  40. else:
  41. str_number = str(number)
  42. if str_number[0] == '-':
  43. sign = '-'
  44. str_number = str_number[1:]
  45. # decimal part
  46. if '.' in str_number:
  47. int_part, dec_part = str_number.split('.')
  48. if decimal_pos is not None:
  49. dec_part = dec_part[:decimal_pos]
  50. else:
  51. int_part, dec_part = str_number, ''
  52. if decimal_pos is not None:
  53. dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
  54. dec_part = dec_part and decimal_sep + dec_part
  55. # grouping
  56. if use_grouping:
  57. try:
  58. # if grouping is a sequence
  59. intervals = list(grouping)
  60. except TypeError:
  61. # grouping is a single value
  62. intervals = [grouping, 0]
  63. active_interval = intervals.pop(0)
  64. int_part_gd = ''
  65. cnt = 0
  66. for digit in int_part[::-1]:
  67. if cnt and cnt == active_interval:
  68. if intervals:
  69. active_interval = intervals.pop(0) or active_interval
  70. int_part_gd += thousand_sep[::-1]
  71. cnt = 0
  72. int_part_gd += digit
  73. cnt += 1
  74. int_part = int_part_gd[::-1]
  75. return sign + int_part + dec_part