numberformat.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if not number.is_finite():
  47. return str(number)
  48. # Format values with more than 200 digits (an arbitrary cutoff) using
  49. # scientific notation to avoid high memory usage in {:f}'.format().
  50. _, digits, exponent = number.as_tuple()
  51. if abs(exponent) + len(digits) > 200:
  52. number = "{:e}".format(number)
  53. coefficient, exponent = number.split("e")
  54. # Format the coefficient.
  55. coefficient = format(
  56. coefficient,
  57. decimal_sep,
  58. decimal_pos,
  59. grouping,
  60. thousand_sep,
  61. force_grouping,
  62. use_l10n,
  63. )
  64. return "{}e{}".format(coefficient, exponent)
  65. else:
  66. str_number = "{:f}".format(number)
  67. else:
  68. str_number = str(number)
  69. if str_number[0] == "-":
  70. sign = "-"
  71. str_number = str_number[1:]
  72. # decimal part
  73. if "." in str_number:
  74. int_part, dec_part = str_number.split(".")
  75. if decimal_pos is not None:
  76. dec_part = dec_part[:decimal_pos]
  77. else:
  78. int_part, dec_part = str_number, ""
  79. if decimal_pos is not None:
  80. dec_part += "0" * (decimal_pos - len(dec_part))
  81. dec_part = dec_part and decimal_sep + dec_part
  82. # grouping
  83. if use_grouping:
  84. try:
  85. # if grouping is a sequence
  86. intervals = list(grouping)
  87. except TypeError:
  88. # grouping is a single value
  89. intervals = [grouping, 0]
  90. active_interval = intervals.pop(0)
  91. int_part_gd = []
  92. cnt = 0
  93. for digit in int_part[::-1]:
  94. if cnt and cnt == active_interval:
  95. if intervals:
  96. active_interval = intervals.pop(0) or active_interval
  97. int_part_gd.append(thousand_sep[::-1])
  98. cnt = 0
  99. int_part_gd.append(digit)
  100. cnt += 1
  101. int_part = "".join(int_part_gd)[::-1]
  102. return sign + int_part + dec_part