#include <iomanip>
#include <sstream>
#include "Rtypes.h"
#include "TMemStatHelpers.h"
using namespace std;
string Memstat::dig2bytes(Long64_t bytes)
{
ostringstream ss;
ss << fixed;
if(bytes < 0) {
ss << '-';
bytes = -bytes;
}
static const long kB = 1024L;
static const long lMB = kB * kB;
static const long lGB = lMB * kB;
if(bytes < kB)
ss << bytes << " B";
else if(bytes < (10L * kB))
ss << setprecision(2) << ((double)bytes / (float)kB) << " kB";
else if(bytes < (100L * kB))
ss << setprecision(1) << ((double)bytes / (float)kB) << " kB";
else if(bytes < lMB)
ss << setprecision(0) << ((double)bytes / (float)kB) << " kB";
else if(bytes < (10L * lMB))
ss << setprecision(2) << ((double)bytes / (float)lMB) << " MB";
else if(bytes < (100L * lMB))
ss << setprecision(1) << ((double)bytes / (float)lMB) << " MB";
else if(bytes < lGB)
ss << setprecision(0) << ((double)bytes / (float)lMB) << " MB";
else
ss << setprecision(2) << ((double)bytes / (float)lGB) << " GB";
return ss.str();
}