1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-20 13:16:53 +00:00

(trunk libT) #3521 "rounding issue in tr_truncd()" -- fixed.

This commit is contained in:
Charles Kerr 2010-09-22 16:09:36 +00:00
parent 70933cfae1
commit b63d64e0e3
2 changed files with 6 additions and 3 deletions

View file

@ -389,6 +389,9 @@ test_truncd( void )
tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) ); tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) );
check( !strcmp( buf, "403650.6562" ) ); check( !strcmp( buf, "403650.6562" ) );
tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) );
check( !strcmp( buf, "2.15" ) );
return 0; return 0;
} }

View file

@ -1385,8 +1385,9 @@ tr_parseNumberRange( const char * str_in, int len, int * setmeCount )
double double
tr_truncd( double x, int decimal_places ) tr_truncd( double x, int decimal_places )
{ {
const int i = (int) pow( 10, decimal_places ); const int i = (int) pow( 10, decimal_places );
const double x2 = (int64_t)(x * i); const double xup = x * i;
const double x2 = (int64_t)(xup);
return x2 / i; return x2 / i;
} }
@ -1394,7 +1395,6 @@ char*
tr_strtruncd( char * buf, double x, int precision, size_t buflen ) tr_strtruncd( char * buf, double x, int precision, size_t buflen )
{ {
tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) ); tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) );
return buf; return buf;
} }