From 717518400d6709cf7e9bebe4a9424eb3b1ebd60d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 20 Dec 2025 18:02:54 +0200 Subject: [PATCH] Add locale support for decimal separator in intword --- src/humanize/number.py | 4 +++- tests/test_i18n.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 2ca7a69..77b79ab 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -261,7 +261,9 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str: singular, plural = human_powers[ordinal] unit = _ngettext(singular, plural, math.ceil(rounded_value)) - return f"{negative_prefix}{format % rounded_value} {unit}" + decimal_sep = decimal_separator() + number = (format % rounded_value).replace(".", decimal_sep) + return f"{negative_prefix}{number} {unit}" def apnumber(value: NumberOrString) -> str: diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 6c16c94..319a55d 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -91,6 +91,12 @@ def test_naturaldelta() -> None: @pytest.mark.parametrize( "locale, number, expected_result", [ + # Italian uses comma as decimal separator + ("it_IT", 1_000_000, "1,0 milione"), + ("it_IT", 1_200_000, "1,2 milioni"), + ("it_IT", 1_000_000_000, "1,0 miliardo"), + ("it_IT", 3_500_000_000, "3,5 miliardi"), + # Spanish uses dot as decimal separator ("es_ES", 1_000_000, "1.0 millón"), ("es_ES", 3_500_000, "3.5 millones"), ("es_ES", 1_000_000_000, "1.0 billón"),