]> granicus.if.org Git - transmission/commitdiff
(trunk gtk) #1881: Displayed ratio should be truncated, not rounded
authorCharles Kerr <charles@transmissionbt.com>
Wed, 4 Mar 2009 15:37:54 +0000 (15:37 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Wed, 4 Mar 2009 15:37:54 +0000 (15:37 +0000)
gtk/util.c

index 11a3c3b29c64f8ac9d2c0f1dfefcb5fd7b6fb789..bca6c743417715ee5c1417c553a77185a26b7660 100644 (file)
 #include "tr-prefs.h"
 #include "util.h"
 
+static void
+printf_double_without_rounding( char * buf, int buflen, double d, int places )
+{
+    char * pch;
+    char tmp[128];
+    int len;
+    g_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';
+}
+
 char*
-tr_strlratio( char * buf,
-              double ratio,
-              size_t buflen )
+tr_strlratio( char * buf, double ratio, size_t buflen )
 {
     if( (int)ratio == TR_RATIO_NA )
         g_strlcpy( buf, _( "None" ), buflen );
     else if( (int)ratio == TR_RATIO_INF )
         g_strlcpy( buf, "\xE2\x88\x9E", buflen );
     else if( ratio < 10.0 )
-        g_snprintf( buf, buflen, "%'.2f", ratio );
+        printf_double_without_rounding( buf, buflen, ratio, 2 );
     else if( ratio < 100.0 )
-        g_snprintf( buf, buflen, "%'.1f", ratio );
+        printf_double_without_rounding( buf, buflen, ratio, 1 );
     else
         g_snprintf( buf, buflen, "%'.0f", ratio );
     return buf;