//QT版本
static QString humanReadableSize(uint64_t bytes)
{
const char* units[] = { "B", "KB", "MB", "GB", "TB" };
double size = static_cast<double>(bytes);
int unitIndex = 0;
while (size >= 1024 && unitIndex < 4) {
size /= 1024.0;
++unitIndex;
}
return QString::number(size, 'f', 2) + units[unitIndex];
}
//std::string版本
static std::string humanReadableSize(uint64_t bytes) {
const char* units[] = { "B", "KB", "MB", "GB", "TB" };
double size = static_cast<double>(bytes);
int unitIndex = 0;
// 这里用 1024 进制,如果你想用 1000 进制,把 1024 改成 1000
while (size >= 1024 && unitIndex < 4) {
size /= 1024.0;
++unitIndex;
}
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << size << units[unitIndex];
return oss.str();
}
字节转换成human习惯
于 2025-09-23 19:04:33 首次发布
3万+

被折叠的 条评论
为什么被折叠?



