Skip to content

Commit e705e43

Browse files
authored
Merge pull request #41 from vishket/update-intword
2 parents 5dc4b82 + 4c76d35 commit e705e43

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/humanize/number.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,36 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
205205
except (TypeError, ValueError):
206206
return str(value)
207207

208+
if value < 0:
209+
value *= -1
210+
negative_prefix = "-"
211+
else:
212+
negative_prefix = ""
213+
208214
if value < powers[0]:
209-
return str(value)
215+
return negative_prefix + str(value)
210216
for ordinal, power in enumerate(powers[1:], 1):
211217
if value < power:
212218
chopped = value / float(powers[ordinal - 1])
213219
if float(format % chopped) == float(10**3):
214220
chopped = value / float(powers[ordinal])
215221
singular, plural = human_powers[ordinal]
216222
return (
217-
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
223+
negative_prefix
224+
+ " ".join(
225+
[format, _ngettext(singular, plural, math.ceil(chopped))]
226+
)
218227
) % chopped
219228
else:
220229
singular, plural = human_powers[ordinal - 1]
221230
return (
222-
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
231+
negative_prefix
232+
+ " ".join(
233+
[format, _ngettext(singular, plural, math.ceil(chopped))]
234+
)
223235
) % chopped
224-
return str(value)
236+
237+
return negative_prefix + str(value)
225238

226239

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

tests/test_number.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,31 @@ def test_intword_powers() -> None:
7575
@pytest.mark.parametrize(
7676
"test_args, expected",
7777
[
78+
(["0"], "0"),
7879
(["100"], "100"),
80+
(["-100"], "-100"),
7981
(["1000"], "1.0 thousand"),
8082
(["12400"], "12.4 thousand"),
8183
(["12490"], "12.5 thousand"),
8284
(["1000000"], "1.0 million"),
85+
(["-1000000"], "-1.0 million"),
8386
(["1200000"], "1.2 million"),
8487
(["1290000"], "1.3 million"),
8588
(["999999999"], "1.0 billion"),
8689
(["1000000000"], "1.0 billion"),
90+
(["-1000000000"], "-1.0 billion"),
8791
(["2000000000"], "2.0 billion"),
8892
(["999999999999"], "1.0 trillion"),
8993
(["1000000000000"], "1.0 trillion"),
9094
(["6000000000000"], "6.0 trillion"),
95+
(["-6000000000000"], "-6.0 trillion"),
9196
(["999999999999999"], "1.0 quadrillion"),
9297
(["1000000000000000"], "1.0 quadrillion"),
9398
(["1300000000000000"], "1.3 quadrillion"),
99+
(["-1300000000000000"], "-1.3 quadrillion"),
94100
(["3500000000000000000000"], "3.5 sextillion"),
95101
(["8100000000000000000000000000000000"], "8.1 decillion"),
102+
(["-8100000000000000000000000000000000"], "-8.1 decillion"),
96103
([None], "None"),
97104
(["1230000", "%0.2f"], "1.23 million"),
98105
([10**101], "1" + "0" * 101),

0 commit comments

Comments
 (0)