Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ python3 -m pip install -e .
>>> import humanize
>>> humanize.intcomma(12345)
'12,345'
>>> humanize.intcomma(12345, separator="_")
'12_345'
>>> humanize.intword(123455913)
'123.5 million'
>>> humanize.intword(12345591313)
Expand Down
21 changes: 15 additions & 6 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def ordinal(value: NumberOrString, gender: str = "male") -> str:
return f"{value}{P_(*_ORDINAL_SUFFIXES[gender][digit])}"


def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
def intcomma(
value: NumberOrString,
ndigits: int | None = None,
separator: str | None = None,
) -> str:
"""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
Expand All @@ -163,6 +167,8 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
'14,308.4'
>>> intcomma("14308.40", 1)
'14,308.4'
>>> intcomma(1_234_567, separator="_")
'1_234_567'
>>> intcomma(None)
'None'

Expand All @@ -171,17 +177,20 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
Args:
value (int, float, str): Integer or float to convert.
ndigits (int, None): Digits of precision for rounding after the decimal point.
separator (str, None): Thousands separator to use. If `None`, use the active
locale's thousands separator.

Returns:
str: String containing commas every three digits.
"""
import math

thousands_sep = thousands_separator()
locale_thousands_sep = thousands_separator()
thousands_sep = locale_thousands_sep if separator is None else separator
decimal_sep = decimal_separator()
try:
if isinstance(value, str):
value = value.replace(thousands_sep, "").replace(decimal_sep, ".")
value = value.replace(locale_thousands_sep, "").replace(decimal_sep, ".")
if not math.isfinite(float(value)):
return _format_not_finite(float(value))
if "." in value:
Expand All @@ -199,9 +208,9 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str:
result = f"{value:,.{ndigits}f}"
else:
result = f"{value:,}"
if thousands_sep != "," or decimal_sep != ".":
result = result.translate(str.maketrans(",.", thousands_sep + decimal_sep))
return result
integer_part, dot, fractional_part = result.partition(".")
integer_part = integer_part.replace(",", thousands_sep)
return integer_part + (decimal_sep + fractional_part if dot else "")


powers = [10**x for x in (3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 100)]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def test_intcomma() -> None:
try:
humanize.i18n.activate("de_DE")
assert humanize.intcomma(number) == "10.000.000"
assert humanize.intcomma(number, separator="_") == "10_000_000"
assert humanize.intcomma(1_234_567.8901) == "1.234.567,8901"
assert humanize.intcomma(1_234_567.89, separator="_") == "1_234_567,89"
assert humanize.intcomma(1_234_567.89) == "1.234.567,89"
assert humanize.intcomma("1234567,89") == "1.234.567,89"
assert humanize.intcomma("1.234.567,89") == "1.234.567,89"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def test_ordinal(test_input: str, expected: str) -> None:
([1234.5454545, 2], "1,234.55"),
([1234.5454545, 3], "1,234.545"),
([1234.5454545, 10], "1,234.5454545000"),
([1234567, None, "_"], "1_234_567"),
([1234567.89, 2, " "], "1 234 567.89"),
([1234567, None, ""], "1234567"),
([math.nan], "NaN"),
([math.inf], "+Inf"),
([-math.inf], "-Inf"),
Expand Down