Skip to content

Commit 934279e

Browse files
mmaxjrclaude
authored andcommitted
fix(intword): correct decillion-to-googol gap and carry-over rounding
`powers` jumps from decillion (10**33) directly to googol (10**100), so any value in [10**36, 10**100) was chopped into a decillion count that grew arbitrarily large (e.g. 10**50 -> "100000000000000000.0 decillion"), and values that should round up into googol (e.g. 10**100 - 10**93) never carried over. Only advance to the next named unit when the value actually rounds up to it; otherwise there is no unit for that magnitude, so fall back to the plain integer, mirroring how intword() already falls back to a plain value below the smallest unit. Fixes #356 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f971127 commit 934279e

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

‎src/humanize/number.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,24 @@ def intword(value: NumberOrString, format: str = "%.1f") -> str:
282282
chopped = value / power
283283
rounded_value = float(format % chopped)
284284

285-
if not largest_ordinal and rounded_value * power == powers[ordinal + 1]:
286-
# After rounding, we end up just at the next power
287-
ordinal += 1
288-
rounded_value = 1.0
285+
if not largest_ordinal and rounded_value >= 1000:
286+
next_power = powers[ordinal + 1]
287+
if next_power == power * 1000:
288+
# After rounding, we end up just at the next power
289+
ordinal += 1
290+
rounded_value = 1.0
291+
else:
292+
# `powers` has a gap between this unit and the next one (e.g.
293+
# decillion to googol), so there is no unit for "1000+ of the
294+
# current one". Only bump to the next unit if the value is
295+
# close enough to actually round up to it; otherwise there is
296+
# no name for this magnitude, so fall back to the plain number.
297+
next_rounded_value = float(format % (value / next_power))
298+
if next_rounded_value >= 1.0:
299+
ordinal += 1
300+
rounded_value = next_rounded_value
301+
else:
302+
return f"{negative_prefix}{value}"
289303

290304
singular, plural = human_powers[ordinal]
291305
unit = _ngettext(singular, plural, math.ceil(rounded_value))

‎tests/test_number.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,21 @@ def test_intword_powers() -> None:
123123
(["3500000000000000000000"], "3.5 sextillion"),
124124
(["8100000000000000000000000000000000"], "8.1 decillion"),
125125
(["-8100000000000000000000000000000000"], "-8.1 decillion"),
126-
([1_000_000_000_000_000_000_000_000_000_000_000_000], "1000.0 decillion"),
127-
([1_100_000_000_000_000_000_000_000_000_000_000_000], "1100.0 decillion"),
128-
([2_100_000_000_000_000_000_000_000_000_000_000_000], "2100.0 decillion"),
126+
(
127+
[1_000_000_000_000_000_000_000_000_000_000_000_000],
128+
"1000000000000000000000000000000000000",
129+
),
130+
(
131+
[1_100_000_000_000_000_000_000_000_000_000_000_000],
132+
"1100000000000000000000000000000000000",
133+
),
134+
(
135+
[2_100_000_000_000_000_000_000_000_000_000_000_000],
136+
"2100000000000000000000000000000000000",
137+
),
129138
([2e100], "2.0 googol"),
139+
([10**50], str(10**50)),
140+
([10**100 - 10**93], "1.0 googol"),
130141
([None], "None"),
131142
(["1230000", "%0.2f"], "1.23 million"),
132143
([10**100], "1.0 googol"),

0 commit comments

Comments
 (0)