Update the eng() function.

Allow outputting bytes in multiples of 1024.
Reformatting of file.
This commit is contained in:
Holger Vogt 2026-04-28 11:49:13 +02:00
parent b00d8daf58
commit 2c461c4441
1 changed files with 63 additions and 48 deletions

View File

@ -13,8 +13,10 @@
/* Print a floating-point number in engineering notation.
Return string needs to be freed by the caller after its use.
numeric selects e3, e6, e9 etc. or k, M, G etc. */
char *eng(double value, int digits, int numeric)
numeric selects e3, e6, e9 etc. or k, M, G etc.
If flag bytes is set true, numeric is overwritten, bytes
in multiples of 1024 are issued using k, M, G, T, P. */
char *eng(double value, int digits, bool numeric, bool bytes)
{
static char *prefix[] = {
"y", "z", "a", "f", "p", "n", "u", "m", "",
@ -23,10 +25,23 @@ char *eng(double value, int digits, int numeric)
#define PREFIX_END (PREFIX_START+\
(int)((sizeof(prefix)/sizeof(char *)-1)*3))
double display, fract;
int expof10;
char *result, *sign;
if (bytes) {
int i = 0;
// Divide by 1024 until the unit is reached
while (value >= 1024. && i < 5) {
value /= 1024.;
i++;
}
result = tprintf("%.*g %s", digits - 1, value, prefix[i + 8]);
return result;
}
if(value < 0.0) {
sign = "-";
value = -value;