Skip to content

Commit

Permalink
human-readable: also handle num < mult with the same code
Browse files Browse the repository at this point in the history
... just make sure no precision is added.
  • Loading branch information
eworm-de committed Apr 9, 2024
1 parent 836c5dd commit 53a47b6
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions lib/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 53a47b6

Please sign in to comment.