From c736b361190e1133b4d407a6c7615bf8c4d9c262 Mon Sep 17 00:00:00 2001 From: gloit042 Date: Mon, 21 Dec 2020 00:35:21 +0800 Subject: [PATCH] num2str: fix precision loss bug when the fractional part is close to 1 example: The result of num2str(11999999999999, 4, 1, 0, N2S_NONE) should be "12.0G", but current result is "11.0G". Signed-off-by: Jiahao Li --- lib/num2str.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/num2str.c b/lib/num2str.c index 726f1c44..423d97b2 100644 --- a/lib/num2str.c +++ b/lib/num2str.c @@ -112,6 +112,9 @@ done: sprintf(tmp, "%.*f", (int)(maxlen - strlen(tmp) - 1), (double)modulo / (double)thousand); + if (tmp[0] == '1') + num++; + if (asprintf(&buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2], unitprefix[post_index], unitstr[units]) < 0) buf = NULL; -- 2.25.1