Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def naturalsize(
return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_)

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
# 999.999 kB, which formats to "1000.0 kB"). When that happens and a larger
# suffix is available, step up one suffix so the result reads "1.0 MB".
if exp < len(suffix) and abs(float(format % (abs_bytes / (base**exp)))) >= base:
exp += 1
space = "" if gnu else " "
ret: str = format % (bytes_ / (base**exp)) + space + _(suffix[exp - 1])
return ret
9 changes: 9 additions & 0 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
([1.123456789, False, True], "1B"),
([1.123456789 * 10**3, False, True], "1.1K"),
([1.123456789 * 10**6, False, True], "1.1M"),
# Rounding must not leave the mantissa at the base while a larger suffix
# is available: 999999 is 999.999 kB, which the "%.1f" format rounds to
# 1000.0 and must carry into 1.0 MB rather than render as "1000.0 kB".
([999999], "1.0 MB"),
([999999999], "1.0 GB"),
([999999999999], "1.0 TB"),
([1024**2 - 1, True], "1.0 MiB"),
([1024**3 - 1, True], "1.0 GiB"),
([1024**2 - 1, False, True], "1.0M"),
],
)
def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> None:
Expand Down