Skip to content

Commit e87f2e2

Browse files
authored
Add locale support for decimal separator in intword (#287)
2 parents 2526999 + 7175184 commit e87f2e2

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/humanize/number.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
261261

262262
singular, plural = human_powers[ordinal]
263263
unit = _ngettext(singular, plural, math.ceil(rounded_value))
264-
return f"{negative_prefix}{format % rounded_value} {unit}"
264+
decimal_sep = decimal_separator()
265+
number = (format % rounded_value).replace(".", decimal_sep)
266+
return f"{negative_prefix}{number} {unit}"
265267

266268

267269
def apnumber(value: NumberOrString) -> str:

tests/test_i18n.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def test_naturaldelta() -> None:
9191
@pytest.mark.parametrize(
9292
"locale, number, expected_result",
9393
[
94+
# Italian uses comma as decimal separator
95+
("it_IT", 1_000_000, "1,0 milione"),
96+
("it_IT", 1_200_000, "1,2 milioni"),
97+
("it_IT", 1_000_000_000, "1,0 miliardo"),
98+
("it_IT", 3_500_000_000, "3,5 miliardi"),
99+
# Spanish uses dot as decimal separator
94100
("es_ES", 1_000_000, "1.0 millón"),
95101
("es_ES", 3_500_000, "3.5 millones"),
96102
("es_ES", 1_000_000_000, "1.0 billón"),

0 commit comments

Comments
 (0)