diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index fb675fdc..063affe0 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -2,9 +2,7 @@ from __future__ import annotations -__lazy_modules__ = {"humanize.i18n", "math"} - -from math import log +__lazy_modules__ = {"humanize.i18n"} from humanize.i18n import _gettext as _ @@ -98,6 +96,8 @@ def naturalsize( if abs_bytes < base: return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_) + from math import log + exp = int(min(log(abs_bytes, base), len(suffix))) # The suffix is chosen from the unrounded byte count, but `format` rounds the # mantissa afterward; rounding can push it up to `base` (e.g. 999999 is