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
4 changes: 3 additions & 1 deletion src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down