aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/printf.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-11-23 18:57:15 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-11-23 18:57:15 +0000
commit1eb05816d30b522894969c0d89522b30a832b473 (patch)
treeb3084deda7887f7826f25086f23a1b9985227172 /src/libutil/printf.c
parentbda6ff4cc2b609a9ffd6d00acd17f67ff7509ca1 (diff)
downloadrspamd-1eb05816d30b522894969c0d89522b30a832b473.tar.gz
rspamd-1eb05816d30b522894969c0d89522b30a832b473.zip
Improve numbers humanisation
Diffstat (limited to 'src/libutil/printf.c')
-rw-r--r--src/libutil/printf.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index c8c6ace42..11143f79f 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -40,11 +40,10 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes)
const gchar *prefixes;
int i, r, remainder, sign;
gint64 divisor;
- gsize baselen, len = last - buf;
+ gsize len = last - buf;
remainder = 0;
- baselen = 1;
if (!bytes) {
divisor = 1000;
prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
@@ -54,22 +53,14 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes)
prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
}
-
#define SCALE2PREFIX(scale) (&prefixes[(scale) * 3])
if (num < 0) {
sign = -1;
num = -num;
- baselen += 2; /* sign, digit */
}
else {
sign = 1;
- baselen += 1; /* digit */
- }
-
- /* Check if enough room for `x y' + suffix + `\0' */
- if (len < baselen + 1) {
- return buf;
}
/*
@@ -82,9 +73,17 @@ rspamd_humanize_number (gchar *buf, gchar *last, gint64 num, gboolean bytes)
num /= divisor;
}
- r = rspamd_snprintf (buf, len, "%L%s",
- sign * (num + (remainder + 50) / 1000),
- SCALE2PREFIX (i));
+ if (remainder == 0 || num > divisor / 2) {
+ r = rspamd_snprintf (buf, len, "%L%s",
+ sign * (num + (remainder + 50) / divisor),
+ SCALE2PREFIX (i));
+ }
+ else {
+ /* Floating point version */
+ r = rspamd_snprintf (buf, len, "%.2f%s",
+ sign * (num + remainder / (gdouble)divisor),
+ SCALE2PREFIX (i));
+ }
#undef SCALE2PREFIX