From 53a47b6d02364dae21784d80865afe3a959d2ef3 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Wed, 22 Mar 2023 11:07:15 +0100 Subject: [PATCH] human-readable: also handle num < mult with the same code ... just make sure no precision is added. --- lib/compat.c | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/compat.c b/lib/compat.c index 29d445fa..02527dbd 100644 --- a/lib/compat.c +++ b/lib/compat.c @@ -182,32 +182,33 @@ char *do_big_num(int64 num, int human_flag, const char *fract) if (human_flag > 1) { int mult = human_flag == 2 ? 1000 : 1024; - if (num >= mult || num <= -mult) { - const char* units = " KMGTPEZY"; - int64 powi = 1; - int powj = 1, precision = 2; - - for (;;) { - if (labs(num / mult) < powi) - break; - - if (units[1] == '\0') - break; - - powi *= mult; - ++units; - } - - for (; precision > 0; precision--) { - powj *= 10; - if (labs(num / powi) < powj) - break; - } - - snprintf(bufs[n], sizeof bufs[0], "%.*f%c", - precision, (double) num / powi, *units); - return bufs[n]; + const char* units = " KMGTPEZY"; + int64 powi = 1; + int powj = 1, precision = 2; + + for (;;) { + if (labs(num / mult) < powi) + break; + + if (units[1] == '\0') + break; + + powi *= mult; + ++units; } + + if (powi == 1) + precision = 0; + + for (; precision > 0; precision--) { + powj *= 10; + if (labs(num / powi) < powj) + break; + } + + snprintf(bufs[n], sizeof bufs[0], "%.*f%c", precision, + (double) num / powi, *units); + return bufs[n]; } s = bufs[n] + sizeof bufs[0] - 1;