Skip to content

Commit 4c76d35

Browse files
committed
refactor
1 parent 520aac1 commit 4c76d35

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/humanize/number.py

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

208-
is_negative = value < 0
209-
210-
if is_negative:
208+
if value < 0:
211209
value *= -1
210+
negative_prefix = "-"
211+
else:
212+
negative_prefix = ""
212213

213214
if value < powers[0]:
214-
return "-" + str(value) if is_negative else str(value)
215+
return negative_prefix + str(value)
215216
for ordinal, power in enumerate(powers[1:], 1):
216217
if value < power:
217218
chopped = value / float(powers[ordinal - 1])
218219
if float(format % chopped) == float(10**3):
219220
chopped = value / float(powers[ordinal])
220221
singular, plural = human_powers[ordinal]
221-
if is_negative:
222-
return (
223-
"-"
224-
+ " ".join(
225-
[format, _ngettext(singular, plural, math.ceil(chopped))]
226-
)
227-
) % chopped
228-
229222
return (
230-
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
223+
negative_prefix
224+
+ " ".join(
225+
[format, _ngettext(singular, plural, math.ceil(chopped))]
226+
)
231227
) % chopped
232228
else:
233229
singular, plural = human_powers[ordinal - 1]
234-
if is_negative:
235-
return (
236-
"-"
237-
+ " ".join(
238-
[format, _ngettext(singular, plural, math.ceil(chopped))]
239-
)
240-
) % chopped
241230
return (
242-
" ".join([format, _ngettext(singular, plural, math.ceil(chopped))])
231+
negative_prefix
232+
+ " ".join(
233+
[format, _ngettext(singular, plural, math.ceil(chopped))]
234+
)
243235
) % chopped
244236

245-
return "-" + str(value) if is_negative else str(value)
237+
return negative_prefix + str(value)
246238

247239

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

0 commit comments

Comments
 (0)