-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.c
More file actions
19 lines (16 loc) · 724 Bytes
/
Copy pathbytes.c
File metadata and controls
19 lines (16 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
char* format_bytes(size_t bytes) {
static char result[50]; // Static buffer to hold the result string
if (bytes < 1024) {
snprintf(result, sizeof(result), "%zu B", bytes);
} else if (bytes < 1024 * 1024) {
snprintf(result, sizeof(result), "%.2f KB", bytes / 1024.0);
} else if (bytes < 1024 * 1024 * 1024) {
snprintf(result, sizeof(result), "%.2f MB", bytes / (1024.0 * 1024.0));
} else if (bytes < 1024LL * 1024 * 1024 * 1024) {
snprintf(result, sizeof(result), "%.2f GB", bytes / (1024.0 * 1024.0 * 1024.0));
} else {
snprintf(result, sizeof(result), "%.2f TB", bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0));
}
return result;
}