Luna/luna/Units.cpp

16 lines
435 B
C++

#include <Format.h>
#include <Units.h>
Result<usize> to_dynamic_unit(usize value, char* buffer, size_t max)
{
if (value < 1024) { return string_format(buffer, max, "%u bytes", value); }
const char* unit_prefixes = "KMGTPE";
while (value > (1024 * 1024))
{
value /= 1024;
unit_prefixes++;
}
return string_format(buffer, max, "%u.%u %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes);
}