commit: 033d7267b9e03bd871987f36ef136a0be6ce2597
parent fcad4b92a088745fe98bc6b8510d52469d1bfa27
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 16 Apr 2025 16:01:48 +0200
lib/humanize: handle negative num
Diffstat:
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/humanize.c b/lib/humanize.c
@@ -6,6 +6,7 @@
#include "../lib/humanize.h"
+#include <math.h> // fabs
#include <stdio.h> // snprintf
struct si_scale
@@ -18,9 +19,10 @@ dtosi(double num, bool iec)
int div = iec ? 1024 : 1000;
const char **prefixes = iec ? iec_prefixes : si_prefixes;
+ bool neg = num < 0;
struct si_scale ret = {
- .number = num,
+ .number = fabs(num),
.prefix = "",
.exponent = 0,
};
@@ -33,5 +35,7 @@ dtosi(double num, bool iec)
ret.prefix = prefixes[ret.exponent];
+ if(neg) ret.number = -ret.number;
+
return ret;
}