diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 063affe0..3ee79e6c 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -90,10 +90,12 @@ def naturalsize( bytes_ = float(value) abs_bytes = abs(bytes_) - if abs_bytes == 1 and not gnu: + if abs_bytes == 1 and not gnu and float(bytes_).is_integer(): return _("%d Byte") % int(bytes_) if abs_bytes < base: + if not float(bytes_).is_integer(): + return f"{format % bytes_}B" if gnu else _("%s Bytes") % (format % bytes_) return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_) from math import log diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 645751b9..44d9aa14 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -74,10 +74,12 @@ ([3000, False, True, "%.3f"], "2.930K"), ([3000000000, False, True, "%.0f"], "3G"), ([10**26 * 30, True, False, "%.3f"], "2.423 RiB"), - ([1.123456789], "1 Bytes"), + ([1.123456789], "1.1 Bytes"), + ([1.5], "1.5 Bytes"), + ([1.5, False, True], "1.5B"), ([1.123456789 * 10**3], "1.1 kB"), ([1.123456789 * 10**6], "1.1 MB"), - ([1.123456789, False, True], "1B"), + ([1.123456789, False, True], "1.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