From c30983ff961e7fe21c572431d9dd4d367ce8d25c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 4 Mar 2009 16:02:50 +0000 Subject: [PATCH] (trunk daemon) #1881: Displayed ratio should be truncated, not rounded --- daemon/remote.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index 9957ef893..4711c2ea2 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -658,6 +658,20 @@ etaToString( char * buf, #define MEGABYTE_FACTOR ( 1024.0 * 1024.0 ) #define GIGABYTE_FACTOR ( 1024.0 * 1024.0 * 1024.0 ) +static void +printf_double_without_rounding( char * buf, int buflen, double d, int places ) +{ + char * pch; + char tmp[128]; + int len; + tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d ); + pch = strchr( tmp, '.' ); + pch += places + 1; + len = MIN( buflen - 1, pch - tmp ); + memcpy( buf, tmp, len ); + buf[len] = '\0'; +} + static char* strlratio2( char * buf, double ratio, size_t buflen ) { @@ -666,9 +680,9 @@ strlratio2( char * buf, double ratio, size_t buflen ) else if( (int)ratio == TR_RATIO_INF ) tr_strlcpy( buf, "Inf", buflen ); else if( ratio < 10.0 ) - tr_snprintf( buf, buflen, "%'.2f", ratio ); + printf_double_without_rounding( buf, buflen, ratio, 2 ); else if( ratio < 100.0 ) - tr_snprintf( buf, buflen, "%'.1f", ratio ); + printf_double_without_rounding( buf, buflen, ratio, 1 ); else tr_snprintf( buf, buflen, "%'.0f", ratio ); return buf;