human.c (430B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "../util.h" 6 7 char * 8 humansize(double n) 9 { 10 static char buf[16]; 11 const char postfixes[] = "BKMGTPE"; 12 size_t i; 13 14 for (i = 0; n >= 1024 && i < strlen(postfixes); i++) 15 n /= 1024; 16 17 if (!i) 18 snprintf(buf, sizeof(buf), "%lu", (unsigned long)n); 19 else 20 snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]); 21 return buf; 22 }