Skip to content

Commit 86d0246

Browse files
committed
human-readable: use dynamic precision length
Let's lower precision for huge numbers. The output used to be: 3.45M -> 46.73M -> 523.11M -> 1.24G -> ... With this change the code always gives the three most significant digits: 3.45M -> 46.7M -> 523M -> 1.24G -> ...
1 parent a8e99c2 commit 86d0246

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

lib/compat.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ char *do_big_num(int64 num, int human_flag, const char *fract)
185185
if (num >= mult || num <= -mult) {
186186
const char* units = " KMGTPEZY";
187187
int64 powi = 1;
188+
int powj = 1, precision = 2;
188189

189190
for (;;) {
190191
if (labs(num / mult) < powi)
@@ -197,8 +198,14 @@ char *do_big_num(int64 num, int human_flag, const char *fract)
197198
++units;
198199
}
199200

200-
snprintf(bufs[n], sizeof bufs[0], "%.2f%c",
201-
(double) num / powi, *units);
201+
for (; precision > 0; precision--) {
202+
powj *= 10;
203+
if (labs(num / powi) < powj)
204+
break;
205+
}
206+
207+
snprintf(bufs[n], sizeof bufs[0], "%.*f%c",
208+
precision, (double) num / powi, *units);
202209
return bufs[n];
203210
}
204211
}

0 commit comments

Comments
 (0)