1
0
mirror of git://git.suckless.org/sbase synced 2024-12-26 08:52:36 +00:00
sbase/libutil/human.c
2014-11-17 16:48:34 +00:00

23 lines
430 B
C

/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <string.h>
#include "../util.h"
char *
humansize(double n)
{
static char buf[16];
const char postfixes[] = "BKMGTPE";
size_t i;
for (i = 0; n >= 1024 && i < strlen(postfixes); i++)
n /= 1024;
if (!i)
snprintf(buf, sizeof(buf), "%lu", (unsigned long)n);
else
snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
return buf;
}