ByteSize.human_readable() should not have decimal point with B suffix (#4777)

ByteSize can only represent whole number of bytes, so displaying a
decimal would be misleading and doesn't really make sense in most cases
where ByteSize is used.

Outside pure mathematics, it is very rare to use non-whole number bytes.

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
Lie Ryan
2022-12-15 22:28:29 +11:00
committed by GitHub
parent 73aa0a8f7d
commit 55fd51fbdf
2 changed files with 7 additions and 4 deletions
+4 -1
View File
@@ -694,7 +694,10 @@ class ByteSize(int):
num = float(self)
for unit in units:
if abs(num) < divisor:
return f'{num:0.1f}{unit}'
if unit == 'B':
return f'{num:0.0f}{unit}'
else:
return f'{num:0.1f}{unit}'
num /= divisor
return f'{num:0.1f}{final_unit}'
+3 -3
View File
@@ -3252,9 +3252,9 @@ def test_frozenset_field_not_convertible():
@pytest.mark.parametrize(
'input_value,output,human_bin,human_dec',
(
('1', 1, '1.0B', '1.0B'),
('1.0', 1, '1.0B', '1.0B'),
('1b', 1, '1.0B', '1.0B'),
('1', 1, '1B', '1B'),
('1.0', 1, '1B', '1B'),
('1b', 1, '1B', '1B'),
('1.5 KB', int(1.5e3), '1.5KiB', '1.5KB'),
('1.5 K', int(1.5e3), '1.5KiB', '1.5KB'),
('1.5 MB', int(1.5e6), '1.4MiB', '1.5MB'),