diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index c495fed..315261a 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -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 diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 04774d9..e695639 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -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: