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,20 +13,35 @@
/* Print a floating-point number in engineering notation. /* Print a floating-point number in engineering notation.
Return string needs to be freed by the caller after its use. Return string needs to be freed by the caller after its use.
numeric selects e3, e6, e9 etc. or k, M, G etc. */ numeric selects e3, e6, e9 etc. or k, M, G etc.
char *eng(double value, int digits, int numeric) 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[] = { static char *prefix[] = {
"y", "z", "a", "f", "p", "n", "u", "m", "", "y", "z", "a", "f", "p", "n", "u", "m", "",
"k", "M", "G", "T", "P", "E", "Z", "Y" "k", "M", "G", "T", "P", "E", "Z", "Y"
}; };
#define PREFIX_END (PREFIX_START+\ #define PREFIX_END (PREFIX_START+\
(int)((sizeof(prefix)/sizeof(char *)-1)*3)) (int)((sizeof(prefix)/sizeof(char *)-1)*3))
double display, fract; double display, fract;
int expof10; int expof10;
char *result, *sign; 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) { if(value < 0.0) {
sign = "-"; sign = "-";
value = -value; value = -value;