#include <iostream>
#include <string>
#include <sstream>
std::string printSizeApproximately(uint64_t n) {
/* 0 */ /* 1 */ /* 2 */ /* 3 */ /* 4 */
static std::string Suffix[] = { std::string("Bytes"), std::string("KiB"), std::string("MiB"), std::string("GiB"), std::string("TiB") };
uint64_t t = n;
uint64_t s = 1;
uint64_t r;
int i = 0;
for (;;) {
r = t;
t >>= 10;
/* out of loop : 2 conditions */
if (t == 0)
break;
if (i == 4)
break;
s <<= 10;
i++;
}
std::stringstream ss;
if (i == 0) {
ss << n << " " << Suffix[i];
} else {
if (r >= 1 && r < (int)(1024 / 100.0)) { /* 1.52KiB */
ss << (int)(n * 100.0 / s) / 100.0 << ' ' << Suffix[i];
} else if (r >= (int)(1024 / 100.0) && r < (int)(1024 / 10.0)) { /* 15.2KiB */
ss << (int)(n * 10.0 / s) / 10.0 << ' ' << Suffix[i];
} else { /* r >= (int)(1024 / 10.0 152KiB */
ss << (int)((double)n / s) << ' ' << Suffix[i];
}
}
std::string output_s = ss.str();
return output_s;
}
int main() {
uint64_t n;
n = 555; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 1123; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* KiB */
n = 18234; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 252344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 3252344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* MiB */
n = 73241344; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 9697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* GiB */
n = 39697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 6439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl; /* TiB */
n = 36439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 8236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
n = 38236439697351323; std::cout << "n = " << n << " : " << printSizeApproximately(n) << std::endl;
return 0;
}
/* end */