humanize_number(3bsd)

πŸ“¦
version 0.12.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– dehumanize_number, humanize_number β€” bestie edition πŸ’–

okay so. we're in humanize_number(3bsd) territory, and honestly? this page is SO useful and nobody talks about it. it's the libbsd version, aka Utility functions from BSD systems β€” you link it with -lbsd and it's lowkey iconic. one function turns a big scary number into something cute and readable, the other one does the reverse. glow up and glow down. πŸ’…


✨ NAME ✨

dehumanize_number, humanize_number β€” format a number into a human readable form and viceversa πŸ¦‹


πŸ“š LIBRARY πŸ“š

Utility functions from BSD systems (libbsd, -lbsd) 🌸


πŸŽ€ SYNOPSIS πŸŽ€

#include <stdlib.h>
(See libbsd(7) for include usage.)

int
dehumanize_number(const char *str, int64_t *result);

int
humanize_number(char *buf, size_t len, int64_t number,
    const char *suffix, int scale, int flags);
not me writing #include <stdlib.h> and then immediately being told to go read libbsd(7) for the actual include tea 😭 do it though. it matters.

🌈 DESCRIPTION 🌈

okay bestie, deep breath, two functions, both slay.

🫢 humanize_number() β€” the glow-up direction

humanize_number() takes that signed 64 bit quantity sitting in number and formats it into buffer. then it appends a space and then suffix at the end. so like, "5 GB" energy. your buffer has to be at least len bytes long, no negotiating.

now here's the ✨magic✨ β€” if the formatted number (suffix included, bestie, the whole fit) would be too long to fit into buffer, it just keeps dividing number by 1024 until it fits. and when it does that, it prefixes suffix with the appropriate SI designator. the way that just works?? obsessed.

the prefixes, in all their glory:

  • k β€” kilo β€” multiplier 1024
  • M β€” mega β€” multiplier 1048576
  • G β€” giga β€” multiplier 1073741824
  • T β€” tera β€” multiplier 1099511627776
  • P β€” peta β€” multiplier 1125899906842624
  • E β€” exa β€” multiplier 1152921504606846976

len must be at least 4 plus the length of suffix, because otherwise the result isn't gonna be useful and we'd simply perish. to force a specific prefix, pass it as scale, where Multiplier = 1024 ^ scale. πŸ”₯

⚠️ you can NOT combine that with any of the scale flags below. pick a lane, bestie.

πŸ‘‘ the flags you can pass in scale

  • HN_AUTOSCALE β€” format the buffer using the lowest multiplier possible. it picks for you. main character behavior.
  • HN_GETSCALE β€” return the prefix index number (aka the number of times number must be divided to fit) instead of formatting it to the buffer. yes, instead. no buffer content for you.

πŸ’… the flags you can pass in flags

  • HN_DECIMAL β€” if the final result is less than 10, display it using one digit.
  • HN_NOSPACE β€” do not put a space between the number and the prefix. snatch that space away.
  • HN_B β€” use 'B' (bytes) as the prefix if the original result does not have a prefix.
  • HN_DIVISOR_1000 β€” divide number with 1000 instead of 1024. metric girlies rise.

πŸ¦‹ dehumanize_number() β€” the glow-down direction

dehumanize_number() parses the string representing an integral value that you hand it in str and stores the numerical value in the integer pointed to by result. and here's the cute part: the string you give it may hold one of those suffixes, which will be interpreted and used to scale up the numerical value riding along with it. it reads the vibe AND the number. fr fr.


⭐ RETURN VALUES ⭐

  • humanize_number() returns the number of characters stored in buffer (excluding the terminating NUL) upon success, or -1 upon failure. and if HN_GETSCALE is specified, the prefix index number will be returned instead. 🌸
  • dehumanize_number() returns 0 if the string was parsed correctly. a -1 is returned to indicate failure and an error code is stored in errno. πŸ’€

🚨 ERRORS 🚨

dehumanize_number() will fail and no number will be stored in result if:

  • [EINVAL] β€” the string in str was empty or carried an unknown suffix. bestie what even is that suffix.
  • [ERANGE] β€” the string in str represented a number that does not fit in result. too much. overflowing. not the vibe.

πŸ‘€ SEE ALSO πŸ‘€

humanize_number(9) β€” go read it, iykyk 🫢


πŸ“œ HISTORY πŸ“œ

  • humanize_number() first appeared in NetBSD 2.0 and FreeBSD 5.3. ✨
  • dehumanize_number() first appeared in NetBSD 5.0. 🌈

okay that's the whole page, you ate it, no crumbs. πŸ“

this one came out of Debian, dated February 9, 2008, and lives under the name humanize_number(3bsd) β€” periodt. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from