Chapter 43. Formatting memory or file sizes

The fmtsize() function formats a numerical value in kilobytes, megabytes, gigabytes, or terabytes, as appropriate:

#include <x/fmtsize.H>

x::fd filedesc;

// ...

std::cout << "File size: " << x::fmtsize(filedesc->stat()->st_size)
          << std::endl;

This is intended for descriptive or display purposes. For small values, x::fmtsie() formats something like 150 bytes. For larger values, Kb, Mb, Gb, or Tb is selected, based on the value. Note that the formatted value is not meant to be exact. fmtsize(1025) returns 1.0 Kb.

parsesize() is the approximate opposite: it parses a std::string that looks like X kb or X Mb and returns a numerical uint64_t that represents the given size, in bytes.