HUMANIZE_NUMBER(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’– NAME πŸ’–

okay bestie, welcome to HUMANIZE_NUMBER(9), straight out of the Kernel Developer’s Manual β€” this one is all about humanize_number and format_bytes, aka human readable numbers ✨. yes, the glow-up for numbers so they don’t look like a cursed wall of digits πŸ’….

✨ SYNOPSIS ✨

int
humanize_number(char *buf, size_t len, uint64_t number,
    const char *suffix, int divisor);

int
format_bytes(char *buf, size_t len, uint64_t number);

πŸ¦‹ DESCRIPTION πŸ¦‹

so humanize_number() takes the unsigned 64-bit quantity in number and formats it into buf 🫢. then it appends a space and then suffix to the end. the supplied buf must be at least len bytes long β€” no cap, that part matters 😌.

if the formatted number (including suffix) is too long to fit into buf, humanize_number() divides number by divisor until it will fit πŸ”. in this case, suffix is prefixed with the appropriate SI designator. suitable values of divisor are 1024 or 1000 to remain consistent with the common meanings of the SI designator prefixes 🌈.

the prefixes are:

  • k β€” kilo, multiplier 1024
  • M β€” mega, multiplier 1048576
  • G β€” giga, multiplier 1073741824
  • T β€” tera, multiplier 1099511627776
  • P β€” peta, multiplier 1125899906842624
  • E β€” exa, multiplier 1152921504606846976
bestie, do not skip this one πŸ™ˆ: the len argument must be at least 4 plus the length of suffix, in order to ensure a useful result in buf. it’s giving β€œhard requirement,” because it is πŸ’€.

the format_bytes() function is a front-end to humanize_number() πŸ’–. it calls the latter with a suffix of B. also, if the suffix in the returned buf would not have a prefix, the suffix is removed. this means that a result of 100000 occurs, instead of 100000 B πŸŽ€.

🌸 RETURN VALUES 🌸

both functions return the number of characters stored in buf (excluding the terminating NUL) upon success, or -1 upon failure 😭. slay when it works, -1 when it doesn’t πŸ’….

πŸŽ€ SEE ALSO πŸŽ€

⭐ HISTORY ⭐

these functions first appeared in NetBSD 1.5 πŸ‘‘.


mwah πŸ’‹ that’s the whole page β€” brought to you by Debian, dated August 7, 2010, and signed HUMANIZE_NUMBER(9). periodt. πŸ’–

πŸ“¦ where this page comes from