Skip to content

Commit

Permalink
Merge pull request #123 from hugovk/118-add-ndigits-to-intcomma
Browse files Browse the repository at this point in the history
Add ndigits option to intcomma
  • Loading branch information
hugovk committed Mar 20, 2020
2 parents 77d943a + 7b78c0b commit 4185315
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
23 changes: 18 additions & 5 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,32 @@ def ordinal(value):
return "%d%s" % (value, t[value % 10])


def intcomma(value):
def intcomma(value, ndigits=None):
"""Converts an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'. To maintain
some compatibility with Django's intcomma, this function also accepts
floats."""
For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
compatibility with Django's intcomma, this function also accepts floats.
Args:
value (int, float, string): Integer or float to convert.
ndigits (int, None): digits of precision for rounding after the decimal point.
Returns:
str: string containing commas every three digits.
"""
try:
if isinstance(value, str):
float(value.replace(",", ""))
else:
float(value)
except (TypeError, ValueError):
return value
orig = str(value)

if ndigits:
orig = "{0:.{1}f}".format(value, ndigits)
else:
orig = str(value)

new = re.sub(r"^(-?\d+)(\d{3})", r"\g<1>,\g<2>", orig)
if orig == new:
return new
Expand Down
43 changes: 27 additions & 16 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,36 @@ def test_ordinal(test_input, expected):


@pytest.mark.parametrize(
"test_input, expected",
"test_args, expected",
[
(100, "100"),
(1000, "1,000"),
(10123, "10,123"),
(10311, "10,311"),
(1000000, "1,000,000"),
(1234567.25, "1,234,567.25"),
("100", "100"),
("1000", "1,000"),
("10123", "10,123"),
("10311", "10,311"),
("1000000", "1,000,000"),
("1234567.1234567", "1,234,567.1234567"),
(None, None),
([100], "100"),
([1000], "1,000"),
([10123], "10,123"),
([10311], "10,311"),
([1000000], "1,000,000"),
([1234567.25], "1,234,567.25"),
(["100"], "100"),
(["1000"], "1,000"),
(["10123"], "10,123"),
(["10311"], "10,311"),
(["1000000"], "1,000,000"),
(["1234567.1234567"], "1,234,567.1234567"),
([None], None),
([14308.40], "14,308.4"),
([14308.40, None], "14,308.4"),
([14308.40, 1], "14,308.4"),
([14308.40, 2], "14,308.40"),
([14308.40, 3], "14,308.400"),
([1234.5454545], "1,234.5454545"),
([1234.5454545, None], "1,234.5454545"),
([1234.5454545, 1], "1,234.5"),
([1234.5454545, 2], "1,234.55"),
([1234.5454545, 3], "1,234.545"),
([1234.5454545, 10], "1,234.5454545000"),
],
)
def test_intcomma(test_input, expected):
assert humanize.intcomma(test_input) == expected
def test_intcomma(test_args, expected):
assert humanize.intcomma(*test_args) == expected


def test_intword_powers():
Expand Down

0 comments on commit 4185315

Please sign in to comment.