From 4f3aece1dd4d253e802e8923d9e80ad2f9fb4c27 Mon Sep 17 00:00:00 2001 From: goulov Date: Fri, 6 Dec 2024 00:08:35 +0000 Subject: [PATCH] mem: enable decimals with percentage --- src/print_mem.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/print_mem.c b/src/print_mem.c index 76a70b90..5ef4954d 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -37,8 +37,9 @@ static int print_bytes_human(char *outwalk, unsigned long bytes, const char *uni return sprintf(outwalk, "%.*f %s", prec, base, iec_symbols[exponent]); } -static int print_percentage(char *outwalk, float percent) { - return snprintf(outwalk, STRING_SIZE, "%.1f%s", percent, pct_mark); +static int print_percentage(char *outwalk, float percent, const int decimals) { + const int prec = decimals > MAX_DECIMALS ? MAX_DECIMALS : decimals; + return snprintf(outwalk, STRING_SIZE, "%.*f%s", prec, percent, pct_mark); } #endif @@ -184,10 +185,10 @@ void print_memory(memory_ctx_t *ctx) { print_bytes_human(string_ram_free, ram_free, ctx->unit, ctx->decimals); print_bytes_human(string_ram_available, ram_available, ctx->unit, ctx->decimals); print_bytes_human(string_ram_shared, ram_shared, ctx->unit, ctx->decimals); - print_percentage(string_percentage_free, 100.0 * ram_free / ram_total); - print_percentage(string_percentage_available, 100.0 * ram_available / ram_total); - print_percentage(string_percentage_used, 100.0 * ram_used / ram_total); - print_percentage(string_percentage_shared, 100.0 * ram_shared / ram_total); + print_percentage(string_percentage_free, 100.0 * ram_free / ram_total, ctx->decimals); + print_percentage(string_percentage_available, 100.0 * ram_available / ram_total, ctx->decimals); + print_percentage(string_percentage_used, 100.0 * ram_used / ram_total, ctx->decimals); + print_percentage(string_percentage_shared, 100.0 * ram_shared / ram_total, ctx->decimals); placeholder_t placeholders[] = { {.name = "%total", .value = string_ram_total},