Skip to content

Commit 54b7b67

Browse files
Improve size_to_string (#990)
Use a float instead to format the size to a string. And if the value is less than 10, show one decimal. Co-authored-by: Christer Fletcher <[email protected]>
1 parent 7ce814a commit 54b7b67

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

usr/lib/linuxmint/mintUpdate/mintUpdate.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@
8888

8989
BLACKLIST_PKG_NAME = 0
9090

91-
GIGABYTE = 1000 ** 3
92-
MEGABYTE = 1000 ** 2
93-
KILOBYTE = 1000
94-
9591

9692
def size_to_string(size):
97-
if (size >= GIGABYTE):
98-
return "%d %s" % (size // GIGABYTE, _("GB"))
99-
if (size >= (MEGABYTE)):
100-
return "%d %s" % (size // MEGABYTE, _("MB"))
101-
if (size >= KILOBYTE):
102-
return "%d %s" % (size // KILOBYTE, _("KB"))
103-
return "%d %s" % (size, _("B"))
93+
f_size = float(size)
94+
units = [_('B'), _('KB'), _('MB'), _('GB')]
95+
for unit in units:
96+
if f_size >= 1000:
97+
f_size /= 1000
98+
else:
99+
break
100+
if f_size >= 10:
101+
return f"{f_size:.0f} {unit}"
102+
else:
103+
return f"{f_size:.1f} {unit}"
104+
104105

105106
def name_search_func(model, column, key, iter):
106107
name = model.get_value(iter, column)

0 commit comments

Comments
 (0)