diff --git a/README.md b/README.md index 81e80801..3756e8b3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/humanize/number.py b/src/humanize/number.py index e5a2612d..f695bda0 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -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 @@ -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' @@ -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: @@ -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)] diff --git a/tests/test_i18n.py b/tests/test_i18n.py index aa9655e6..7f43216a 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -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" diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..bbdafc63 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -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"),