From 3fe42cc99b06b937cd8c75fbafb1e15d3513a9de Mon Sep 17 00:00:00 2001 From: "Theera K." Date: Sat, 14 Sep 2024 18:51:59 +0700 Subject: [PATCH] fix scale == 100 & change prettynum() Signed-off-by: Theera K. --- cmd/arcstat.in | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/cmd/arcstat.in b/cmd/arcstat.in index 1f385766fbaf..1bcb8d7b85bd 100755 --- a/cmd/arcstat.in +++ b/cmd/arcstat.in @@ -396,6 +396,14 @@ def snap_stats(): d[key] = cur[key] +def isint(num): + if isinstance(num, int): + return True + if isinstance(num, float): + return num.is_integer() + return False + + def prettynum(sz, scale, num=0): suffix = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z'] index = 0 @@ -404,19 +412,20 @@ def prettynum(sz, scale, num=0): if scale == -1: return "%s" % num - while abs(num) > scale and index < 5: - num = num / scale - index += 1 + if scale != 100: + while abs(num) > scale and index < 5: + num = num / scale + index += 1 - if (sint == 1 and 0 < abs(num) < 1) or \ - (sint != 1 and abs(num) < 9.95): - format = "%*.1f%s" - arg = num - else: + width = sz - (0 if index == 0 else 1) + intlen = len("%d" % num) + if sint == 1 and isint(num) or width < intlen + 2: format = "%*d%s" arg = round(num) - sf = suffix[index] - return format % (sz - len(sf), arg, sf) + else: + format = "%*.1f%s" + arg = num + return format % (width, arg, suffix[index]) def print_values():