Remove float to int conversion for all units above MB (#4)

Fixes float to int cast that causes poor accuracy when reporting disk and RAM values.
Adds missing dash before RAM percentage.
This commit is contained in:
4ntsu
2019-01-11 12:06:18 +08:00
committed by Gauthier Provost
parent 6fd9f2a3d8
commit ee3540a8be
+6 -3
View File
@@ -40,8 +40,11 @@ def bytes2human(n):
prefix[s] = 1 << (i + 1) * 10 prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols): for s in reversed(symbols):
if n >= prefix[s]: if n >= prefix[s]:
value = int(float(n) / prefix[s]) value = float(n) / prefix[s]
return '%s%s' % (value, s) if s in ['K', 'M']:
return '%d%s' % (int(value), s)
else:
return '%.1f%s' % (value, s)
return "%sB" % n return "%sB" % n
@@ -54,7 +57,7 @@ def cpu_usage():
def mem_usage(): def mem_usage():
usage = psutil.virtual_memory() usage = psutil.virtual_memory()
return "mem: %s / %s %.0f%%" \ return "mem: %s / %s - %.0f%%" \
% (bytes2human(usage.used), bytes2human(usage.total), usage.percent) % (bytes2human(usage.used), bytes2human(usage.total), usage.percent)