FreeBSD の libutil にある humanize_number とexpand_number で K/M/G2019年03月20日 20時37分35秒

FreeBSD の libutil に数字を K/M/G 等の単位に変換する関数がある。du 等で、バイト数を変換するのに使われている。キロやメガで十分に分かるファイルやファイルシステムの大きさの時は、バイト数で表しても読めたが、ギガバイトの大きさのファイルを毎日見る昨今は単位の変換が欠かせない。

humanize_number は数字を表示形式に変換。expand_number は表示形式から数字に変換。

humanize_number - format a number into a human readable form

int
     humanize_number(char *buf, size_t len, int64_t number,
         const char *suffix, int scale, int flags);


expand_number - format a number from human readable form

int
     expand_number(const char *buf, uint64_t *num);
詳しくは、man を参照のこと。