Skip to content

Commit 9c50eb2

Browse files
committed
Add tests for intword pluralization
1 parent 4a79544 commit 9c50eb2

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/humanize/number.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"""Humanizing functions for numbers."""
44

5+
import math
56
import re
67
from fractions import Fraction
78

@@ -185,12 +186,12 @@ def intword(value, format="%.1f"):
185186
chopped = value / float(powers[ordinal])
186187
singular, plural = human_powers[ordinal]
187188
return (
188-
" ".join([format, ngettext(singular, plural, chopped)])
189+
" ".join([format, ngettext(singular, plural, math.ceil(chopped))])
189190
) % chopped
190191
else:
191192
singular, plural = human_powers[ordinal - 1]
192193
return (
193-
" ".join([format, ngettext(singular, plural, chopped)])
194+
" ".join([format, ngettext(singular, plural, math.ceil(chopped))])
194195
) % chopped
195196
return str(value)
196197

tests/test_i18n.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ def test_intcomma():
4747
assert humanize.intcomma(number) == "10,000,000"
4848

4949

50+
@pytest.mark.parametrize(
51+
("locale", "number", "expected_result"),
52+
(
53+
("es_ES", 1000000, "1.0 millón"),
54+
("es_ES", 3500000, "3.5 millones"),
55+
("es_ES", 1000000000, "1.0 billón"),
56+
("es_ES", 1200000000, "1.2 billones"),
57+
("es_ES", 1000000000000, "1.0 trillón"),
58+
("es_ES", 6700000000000, "6.7 trillones"),
59+
),
60+
)
61+
def test_intword_plurals(locale, number, expected_result):
62+
try:
63+
humanize.i18n.activate(locale)
64+
except FileNotFoundError:
65+
pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh")
66+
else:
67+
assert humanize.intword(number) == expected_result
68+
finally:
69+
humanize.i18n.deactivate()
70+
71+
5072
def test_default_locale_path_defined__file__():
5173
i18n = importlib.import_module("humanize.i18n")
5274
assert i18n._get_default_locale_path() is not None

0 commit comments

Comments
 (0)