Skip to content

Commit 67d6240

Browse files
committed
human-readable: also handle num >= mult with the same code
... just make sure no precision is added.
1 parent 7961b89 commit 67d6240

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

lib/compat.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -182,32 +182,33 @@ char *do_big_num(int64 num, int human_flag, const char *fract)
182182
if (human_flag > 1) {
183183
int mult = human_flag == 2 ? 1000 : 1024;
184184

185-
if (num >= mult || num <= -mult) {
186-
const char* units = " KMGTPEZY";
187-
int64 powi = 1;
188-
int powj = 1, precision = 2;
189-
190-
for (;;) {
191-
if (labs(num / mult) < powi)
192-
break;
193-
194-
if (units[1] == '\0')
195-
break;
196-
197-
powi *= mult;
198-
++units;
199-
}
200-
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);
209-
return bufs[n];
185+
const char* units = " KMGTPEZY";
186+
int64 powi = 1;
187+
int powj = 1, precision = 2;
188+
189+
for (;;) {
190+
if (labs(num / mult) < powi)
191+
break;
192+
193+
if (units[1] == '\0')
194+
break;
195+
196+
powi *= mult;
197+
++units;
210198
}
199+
200+
if (powi == 1)
201+
precision = 0;
202+
203+
for (; precision > 0; precision--) {
204+
powj *= 10;
205+
if (labs(num / powi) < powj)
206+
break;
207+
}
208+
209+
snprintf(bufs[n], sizeof bufs[0], "%.*f%c",
210+
precision, (double) num / powi, *units);
211+
return bufs[n];
211212
}
212213

213214
s = bufs[n] + sizeof bufs[0] - 1;

0 commit comments

Comments
 (0)