Fix: it is impossible to have 0.1 byte as byte is the smallest unit

This commit is contained in:
KnugiHK
2025-05-17 19:26:18 +08:00
parent 6a0fca3e9d
commit e408c31415
2 changed files with 5 additions and 4 deletions

View File

@@ -85,8 +85,8 @@ def bytes_to_readable(size_bytes: int) -> str:
Returns:
A human-readable string representing the file size.
"""
if size_bytes == 0:
return "0B"
if size_bytes < 1024:
return f"{size_bytes} B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)

View File

@@ -23,8 +23,8 @@ def test_convert_time_unit():
class TestBytesToReadable:
assert bytes_to_readable(0) == "0B"
assert bytes_to_readable(500) == "500.0 B"
assert bytes_to_readable(0) == "0 B"
assert bytes_to_readable(500) == "500 B"
assert bytes_to_readable(1024) == "1.0 KB"
assert bytes_to_readable(2048) == "2.0 KB"
assert bytes_to_readable(1536) == "1.5 KB"
@@ -40,6 +40,7 @@ class TestBytesToReadable:
class TestReadableToBytes:
def test_conversion(self):
assert readable_to_bytes("0B") == 0
assert readable_to_bytes("100B") == 100
assert readable_to_bytes("50 B") == 50
assert readable_to_bytes("1KB") == 1024