From ee3540a8be6ca8ca38c4b5b938bc065bdc0c3a80 Mon Sep 17 00:00:00 2001 From: 4ntsu <46565592+4ntsu@users.noreply.github.com> Date: Fri, 11 Jan 2019 04:06:18 +0000 Subject: [PATCH] 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. --- bin/sys-oled | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/sys-oled b/bin/sys-oled index 231c875..a697402 100755 --- a/bin/sys-oled +++ b/bin/sys-oled @@ -40,8 +40,11 @@ def bytes2human(n): prefix[s] = 1 << (i + 1) * 10 for s in reversed(symbols): if n >= prefix[s]: - value = int(float(n) / prefix[s]) - return '%s%s' % (value, s) + value = float(n) / prefix[s] + if s in ['K', 'M']: + return '%d%s' % (int(value), s) + else: + return '%.1f%s' % (value, s) return "%sB" % n @@ -54,7 +57,7 @@ def cpu_usage(): def mem_usage(): usage = psutil.virtual_memory() - return "mem: %s / %s %.0f%%" \ + return "mem: %s / %s - %.0f%%" \ % (bytes2human(usage.used), bytes2human(usage.total), usage.percent)