From: Jens Axboe Date: Thu, 16 Sep 2010 12:56:23 +0000 (+0200) Subject: num2str fixes X-Git-Tag: fio-1.44-rc1~5 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=05463816cb6944649ac152283a9d02629ff91c0d num2str fixes Signed-off-by: Jens Axboe --- diff --git a/lib/num2str.c b/lib/num2str.c index e114cdf8..559cbeb5 100644 --- a/lib/num2str.c +++ b/lib/num2str.c @@ -10,8 +10,8 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2) char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' }; unsigned int thousand[] = { 1000, 1024 }; unsigned int modulo, decimals; - int post_index; - char tmp[32], fmt[8]; + int post_index, carry = 0; + char tmp[32]; char *buf; buf = malloc(128); @@ -27,6 +27,7 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2) modulo = num % thousand[!!pow2]; num /= thousand[!!pow2]; + carry = modulo >= thousand[!!pow2] / 2; post_index++; } @@ -38,11 +39,20 @@ done: sprintf(tmp, "%lu", num); decimals = maxlen - strlen(tmp); - if (decimals <= 1) + if (decimals <= 1) { + if (carry) + num++; goto done; + } + + do { + sprintf(tmp, "%u", modulo); + if (strlen(tmp) <= decimals - 1) + break; + + modulo = (modulo + 9) / 10; + } while (1); - sprintf(fmt, "%%.%uu", decimals - 1); - sprintf(tmp, fmt, modulo); - sprintf(buf, "%lu.%s%c", num, tmp, postfix[post_index]); + sprintf(buf, "%lu.%u%c", num, modulo, postfix[post_index]); return buf; }