#include "size_info.h" static const char *size_unit_map[] = {"B", "KB", "MB", "GB"}; size_info_t num2sizeinfo(const unsigned long long n) { size_info_t size_info = {0}; long double tmp = (long double)n; if ((unsigned long)tmp / 1024 == 0) { size_info.unit = SIZE_B; } else { tmp /= 1024; if ((unsigned long)tmp / 1024 == 0) { size_info.unit = SIZE_KB; } else { tmp /= 1024; if ((unsigned long)tmp / 1024 == 0) { size_info.unit = SIZE_MB; } else { tmp /= 1024; size_info.unit = SIZE_GB; } } } size_info.n = tmp; size_info.unit_str = size_unit_map[size_info.unit]; return size_info; }