Created
November 26, 2023 10:00
-
-
Save ZJUGuoShuai/a41029370ad2e234688b0c4a92885f5b to your computer and use it in GitHub Desktop.
将字节数打印为人类易读的格式(KB/MB/GB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct HumanReadable | |
{ | |
std::uintmax_t size{}; | |
private: | |
friend std::ostream& operator<<(std::ostream& os, HumanReadable hr) | |
{ | |
int o{}; | |
double mantissa = hr.size; | |
for (; mantissa >= 1024.; mantissa /= 1024., ++o); | |
os << std::ceil(mantissa * 10.) / 10. << "BKMGTPE"[o]; | |
return o ? os << "B (" << hr.size << ')' : os; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
用法示例:
可能的输出:
参考:std::filesystem::file_size - cppreference.com