numberformat.py 3.7 KB

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