diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 0bf428558..21e190389 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -389,6 +389,9 @@ test_truncd( void ) tr_snprintf( buf, sizeof( buf ), "%.4f", tr_truncd( 403650.656250, 4 ) ); check( !strcmp( buf, "403650.6562" ) ); + tr_snprintf( buf, sizeof( buf ), "%.2f", tr_truncd( 2.15, 2 ) ); + check( !strcmp( buf, "2.15" ) ); + return 0; } diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 2aa165ca9..c76d99881 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1385,8 +1385,9 @@ tr_parseNumberRange( const char * str_in, int len, int * setmeCount ) double tr_truncd( double x, int decimal_places ) { - const int i = (int) pow( 10, decimal_places ); - const double x2 = (int64_t)(x * i); + const int i = (int) pow( 10, decimal_places ); + const double xup = x * i; + const double x2 = (int64_t)(xup); return x2 / i; } @@ -1394,7 +1395,6 @@ char* tr_strtruncd( char * buf, double x, int precision, size_t buflen ) { tr_snprintf( buf, buflen, "%.*f", precision, tr_truncd( x, precision ) ); - return buf; }