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

This commit is contained in:
Charles Kerr 2009-07-14 20:09:46 +00:00
parent cbf9580ec2
commit e5ff7d8f3e
7 changed files with 34 additions and 33 deletions

View File

@ -235,8 +235,8 @@ getStatusStr( const tr_stat * st,
{
tr_snprintf( buf, buflen,
"Verifying local files (%.2f%%, %.2f%% valid)",
100 * st->recheckProgress,
100.0 * st->percentDone );
tr_truncd( 100 * st->recheckProgress, 2 ),
tr_truncd( 100 * st->percentDone, 2 ) );
}
else if( st->activity & TR_STATUS_DOWNLOAD )
{
@ -246,7 +246,7 @@ getStatusStr( const tr_stat * st,
buf, buflen,
"Progress: %.1f%%, dl from %d of %d peers (%.0f KB/s), "
"ul to %d (%.0f KB/s) [%s]",
st->percentDone * 100.0,
tr_truncd( 100 * st->percentDone, 1 ),
st->peersSendingToUs,
st->peersConnected,
st->pieceDownloadSpeed,

View File

@ -795,9 +795,9 @@ refreshInfo( struct DetailsImpl * di, tr_torrent ** torrents, int n )
tr_strlsize( total, haveUnchecked + haveValid, sizeof( total ) );
tr_strlsize( unver, haveUnchecked, sizeof( unver ) );
if( haveUnchecked )
g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%); %3$s Unverified" ), total, ratio, unver );
g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%); %3$s Unverified" ), total, tr_truncd( ratio, 1 ), unver );
else
g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%)" ), total, ratio );
g_snprintf( buf, sizeof( buf ), _( "%1$s (%2$.1f%%)" ), total, tr_truncd( ratio, 1 ) );
str = buf;
}
}

View File

@ -18,7 +18,6 @@
#include <gtk/gtk.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> /* tr_getRatio */
#include "file-list.h"
#include "hig.h"

View File

@ -11,10 +11,11 @@
*/
#include "assert.h"
#include <string.h> /* strcmp */
#include <string.h> /* strcmp() */
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> /* tr_truncd() */
#include "hig.h"
#include "icons.h"
#include "torrent-cell-renderer.h"
@ -61,7 +62,7 @@ getProgressString( const tr_torrent * tor,
_( "%1$s of %2$s (%3$.2f%%)" ),
tr_strlsize( buf1, haveTotal, sizeof( buf1 ) ),
tr_strlsize( buf2, torStat->sizeWhenDone, sizeof( buf2 ) ),
torStat->percentDone * 100.0 );
tr_truncd( torStat->percentDone * 100.0, 2 ) );
}
else if( !isSeed )
{
@ -74,7 +75,7 @@ getProgressString( const tr_torrent * tor,
_( "%1$s of %2$s (%3$.2f%%), uploaded %4$s (Ratio: %5$s)" ),
tr_strlsize( buf1, haveTotal, sizeof( buf1 ) ),
tr_strlsize( buf2, info->totalSize, sizeof( buf2 ) ),
torStat->percentComplete * 100.0,
tr_truncd( torStat->percentComplete * 100.0, 2 ),
tr_strlsize( buf3, torStat->uploadedEver, sizeof( buf3 ) ),
tr_strlratio( buf4, torStat->ratio, sizeof( buf4 ) ) );
}
@ -175,9 +176,8 @@ getShortStatusString( const tr_stat * torStat )
case TR_STATUS_CHECK:
g_string_append_printf( gstr,
_(
"Verifying local data (%.1f%% tested)" ),
torStat->recheckProgress * 100.0 );
_( "Verifying local data (%.1f%% tested)" ),
tr_truncd( torStat->recheckProgress * 100.0, 1 ) );
break;
case TR_STATUS_DOWNLOAD:

View File

@ -29,6 +29,7 @@
#include <glib/gi18n.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> /* tr_truncd() */
#include "tr-prefs.h"
#include "tr-torrent.h"
@ -229,16 +230,14 @@ tr_torrent_status_str( TrTorrent * gtor )
{
case TR_STATUS_CHECK_WAIT:
top =
g_strdup_printf( _(
"Waiting to verify local data (%.1f%% tested)" ),
st->recheckProgress * 100.0 );
g_strdup_printf( _( "Waiting to verify local data (%.1f%% tested)" ),
tr_truncd( 100 * st->recheckProgress, 1 ) );
break;
case TR_STATUS_CHECK:
top =
g_strdup_printf( _(
"Verifying local data (%.1f%% tested)" ),
st->recheckProgress * 100.0 );
g_strdup_printf( _( "Verifying local data (%.1f%% tested)" ),
tr_truncd( 100 * st->recheckProgress, 1 ) );
break;
case TR_STATUS_DOWNLOAD:

View File

@ -17,6 +17,7 @@
#include <assert.h>
#include <ctype.h> /* isalpha, tolower */
#include <errno.h>
#include <math.h> /* pow */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@ -1325,20 +1326,12 @@ tr_parseNumberRange( const char * str_in, int len, int * setmeCount )
****
***/
static void
printf_double_without_rounding( char * buf, int buflen, double d, int places )
double
tr_truncd( double x, int decimal_places )
{
char * pch;
char tmp[128];
int len;
tr_snprintf( tmp, sizeof( tmp ), "%'.64f", d );
pch = tmp;
while( isdigit( *pch ) ) ++pch; /* walk to the decimal point */
++pch; /* walk over the decimal point */
pch += places;
len = MIN( buflen - 1, pch - tmp );
memcpy( buf, tmp, len );
buf[len] = '\0';
const int i = (int) pow( 10, decimal_places );
double x2 = (int)(x*i);
return x2 / i;
}
char*
@ -1349,9 +1342,9 @@ tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity )
else if( (int)ratio == TR_RATIO_INF )
tr_strlcpy( buf, infinity, buflen );
else if( ratio < 10.0 )
printf_double_without_rounding( buf, buflen, ratio, 2 );
tr_snprintf( buf, buflen, "%.2f", tr_truncd( ratio, 2 ) );
else if( ratio < 100.0 )
printf_double_without_rounding( buf, buflen, ratio, 1 );
tr_snprintf( buf, buflen, "%.1f", tr_truncd( ratio, 1 ) );
else
tr_snprintf( buf, buflen, "%'.0f", ratio );
return buf;

View File

@ -392,6 +392,16 @@ int tr_ptr2int( void* );
void* tr_int2ptr( int );
/* truncate a double value at a given number of decimal places.
this can be used to prevent a printf() call from rounding up:
call with the decimal_places argument equal to the number of
decimal places in the printf()'s precision:
- printf("%.2f%%", 99.999 ) ==> "100.00%"
- printf("%.2f%%", tr_truncd(99.999, 2)) ==> "99.99%"
*/
double tr_truncd( double x, int decimal_places );
/**
* @param buf the buffer to write the string to
* @param buflef buf's size