Changeset 8808


Ignore:
Timestamp:
Jul 14, 2009, 8:09:46 PM (13 years ago)
Author:
charles
Message:

(trunk) #2271: rounding problem in printf()

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/cli.c

    r8721 r8808  
    236236        tr_snprintf( buf, buflen,
    237237                     "Verifying local files (%.2f%%, %.2f%% valid)",
    238                      100 * st->recheckProgress,
    239                      100.0 * st->percentDone );
     238                     tr_truncd( 100 * st->recheckProgress, 2 ),
     239                     tr_truncd( 100 * st->percentDone, 2 ) );
    240240    }
    241241    else if( st->activity & TR_STATUS_DOWNLOAD )
     
    247247            "Progress: %.1f%%, dl from %d of %d peers (%.0f KB/s), "
    248248            "ul to %d (%.0f KB/s) [%s]",
    249             st->percentDone * 100.0,
     249            tr_truncd( 100 * st->percentDone, 1 ),
    250250            st->peersSendingToUs,
    251251            st->peersConnected,
  • trunk/gtk/details.c

    r8764 r8808  
    796796            tr_strlsize( unver, haveUnchecked,             sizeof( unver ) );
    797797            if( haveUnchecked )
    798                 g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%); %3$s Unverified" ), total, ratio, unver );
     798                g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%); %3$s Unverified" ), total, tr_truncd( ratio, 1 ), unver );
    799799            else
    800                 g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%)" ), total, ratio );
     800                g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%)" ), total, tr_truncd( ratio, 1 ) );
    801801            str = buf;
    802802        }
  • trunk/gtk/file-list.c

    r8729 r8808  
    1919
    2020#include <libtransmission/transmission.h>
    21 #include <libtransmission/utils.h> /* tr_getRatio */
    2221
    2322#include "file-list.h"
  • trunk/gtk/torrent-cell-renderer.c

    r8389 r8808  
    1212
    1313#include "assert.h"
    14 #include <string.h> /* strcmp */
     14#include <string.h> /* strcmp() */
    1515#include <gtk/gtk.h>
    1616#include <glib/gi18n.h>
    1717#include <libtransmission/transmission.h>
     18#include <libtransmission/utils.h> /* tr_truncd() */
    1819#include "hig.h"
    1920#include "icons.h"
     
    6263            tr_strlsize( buf1, haveTotal, sizeof( buf1 ) ),
    6364            tr_strlsize( buf2, torStat->sizeWhenDone, sizeof( buf2 ) ),
    64             torStat->percentDone * 100.0 );
     65            tr_truncd( torStat->percentDone * 100.0, 2 ) );
    6566    }
    6667    else if( !isSeed )
     
    7576            tr_strlsize( buf1, haveTotal, sizeof( buf1 ) ),
    7677            tr_strlsize( buf2, info->totalSize, sizeof( buf2 ) ),
    77             torStat->percentComplete * 100.0,
     78            tr_truncd( torStat->percentComplete * 100.0, 2 ),
    7879            tr_strlsize( buf3, torStat->uploadedEver, sizeof( buf3 ) ),
    7980            tr_strlratio( buf4, torStat->ratio, sizeof( buf4 ) ) );
     
    176177        case TR_STATUS_CHECK:
    177178            g_string_append_printf( gstr,
    178                                     _(
    179                                         "Verifying local data (%.1f%% tested)" ),
    180                                     torStat->recheckProgress * 100.0 );
     179                                    _( "Verifying local data (%.1f%% tested)" ),
     180                                    tr_truncd( torStat->recheckProgress * 100.0, 1 ) );
    181181            break;
    182182
  • trunk/gtk/tr-torrent.c

    r8757 r8808  
    3030
    3131#include <libtransmission/transmission.h>
     32#include <libtransmission/utils.h> /* tr_truncd() */
    3233
    3334#include "tr-prefs.h"
     
    230231        case TR_STATUS_CHECK_WAIT:
    231232            top =
    232                 g_strdup_printf( _(
    233                                      "Waiting to verify local data (%.1f%% tested)" ),
    234                                  st->recheckProgress * 100.0 );
     233                g_strdup_printf( _( "Waiting to verify local data (%.1f%% tested)" ),
     234                                 tr_truncd( 100 * st->recheckProgress, 1 ) );
    235235            break;
    236236
    237237        case TR_STATUS_CHECK:
    238238            top =
    239                 g_strdup_printf( _(
    240                                      "Verifying local data (%.1f%% tested)" ),
    241                                  st->recheckProgress * 100.0 );
     239                g_strdup_printf( _( "Verifying local data (%.1f%% tested)" ),
     240                                 tr_truncd( 100 * st->recheckProgress, 1 ) );
    242241            break;
    243242
  • trunk/libtransmission/utils.c

    r8737 r8808  
    1818#include <ctype.h> /* isalpha, tolower */
    1919#include <errno.h>
     20#include <math.h> /* pow */
    2021#include <stdarg.h>
    2122#include <stdio.h>
     
    13261327***/
    13271328
    1328 static void
    1329 printf_double_without_rounding( char * buf, int buflen, double d, int places )
    1330 {
    1331     char * pch;
    1332     char tmp[128];
    1333     int len;
    1334     tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d );
    1335     pch = tmp;
    1336     while( isdigit( *pch ) ) ++pch; /* walk to the decimal point */
    1337     ++pch; /* walk over the decimal point */
    1338     pch += places;
    1339     len = MIN( buflen - 1, pch - tmp );
    1340     memcpy( buf, tmp, len );
    1341     buf[len] = '\0';
     1329double
     1330tr_truncd( double x, int decimal_places )
     1331{
     1332    const int i = (int) pow( 10, decimal_places );
     1333    double x2 = (int)(x*i);
     1334    return x2 / i;
    13421335}
    13431336
     
    13501343        tr_strlcpy( buf, infinity, buflen );
    13511344    else if( ratio < 10.0 )
    1352         printf_double_without_rounding( buf, buflen, ratio, 2 );
     1345        tr_snprintf( buf, buflen, "%.2f", tr_truncd( ratio, 2 ) );
    13531346    else if( ratio < 100.0 )
    1354         printf_double_without_rounding( buf, buflen, ratio, 1 );
     1347        tr_snprintf( buf, buflen, "%.1f", tr_truncd( ratio, 1 ) );
    13551348    else
    13561349        tr_snprintf( buf, buflen, "%'.0f", ratio );
  • trunk/libtransmission/utils.h

    r8698 r8808  
    393393void* tr_int2ptr( int );
    394394
     395/* truncate a double value at a given number of decimal places.
     396   this can be used to prevent a printf() call from rounding up:
     397   call with the decimal_places argument equal to the number of
     398   decimal places in the printf()'s precision:
     399   
     400   - printf("%.2f%%",           99.999    ) ==> "100.00%"
     401   - printf("%.2f%%", tr_truncd(99.999, 2)) ==>  "99.99%"
     402*/
     403double tr_truncd( double x, int decimal_places );
     404
    395405/**
    396406 * @param buf the buffer to write the string to
Note: See TracChangeset for help on using the changeset viewer.