gtorrent / gtorrent-core

Core library of gTorrent which handles everything but UI/UX
GNU General Public License v3.0
39 stars 12 forks source link

Use math instead of if/else #34

Closed fuyukaidesu closed 10 years ago

fuyukaidesu commented 10 years ago

Issue by benwaffle Saturday Jul 19, 2014 at 00:44 GMT Originally opened as https://github.com/gtorrent/gTorrent-legacy/issues/103


from some guy on IRC, can we test + merge this?

if( downloaded <= 0 )
{
    ttd << string();
}
else
{
    const string suffixes[] = { " B", " KB", " MB", " GB" };
    const int maxoffset = sizeof( suffixes ) / sizeof( suffixes[0] );
    int offset = floor( log( downloaded ) / log( 1024 ) ); 
    if( offset >= maxoffset )
        offset = maxoffset - 1;

    ttd << fixed << setprecision(3) << downloaded / pow( 1024, offset ) << suffixes[offset];
}
return ttd.str();