Skip to content

Commit

Permalink
Sane size conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyukaidesu committed Aug 20, 2014
1 parent f064b79 commit 9b7f64a
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Platform.hpp"
#include "Settings.hpp"
#include "Torrent.hpp"

#include <cmath>
#include <libtorrent.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/entry.hpp>
Expand Down Expand Up @@ -48,20 +48,15 @@ std::string getRateString(int64_t file_rate)

std::string getFileSizeString(int64_t file_size)
{
std::ostringstream fss;

if (file_size <= 0)
return std::string();

std::ostringstream fss;
fss << std::setprecision(3);

if (file_size >= (1024 * 1024 * 1024))
fss << file_size / (double)(1024 * 1024 * 1024) << " GB";
else if (file_size >= (1024 * 1024))
fss << (file_size / (double)(1024 * 1024)) << " MB";
else if (file_size >= 1024)
fss << (file_size / (double)1024) << " KB";
else if (file_size > 0)
fss << file_size << "B ";
std::string units[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
int e = ::floor(::log(file_size) / ::log(1024));
fss << (file_size / ::pow(1024, e)) << " " << units[e];
return fss.str();
}

Expand Down

0 comments on commit 9b7f64a

Please sign in to comment.