humanize_number(3bsd)
π 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 readlibbsd(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 β multiplier1024Mβ mega β multiplier1048576Gβ giga β multiplier1073741824Tβ tera β multiplier1099511627776Pβ peta β multiplier1125899906842624Eβ exa β multiplier1152921504606846976
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 timesnumbermust 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β dividenumberwith 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 inbuffer(excluding the terminating NUL) upon success, or-1upon failure. and ifHN_GETSCALEis specified, the prefix index number will be returned instead. πΈdehumanize_number()returns0if the string was parsed correctly. a-1is returned to indicate failure and an error code is stored inerrno. π
π¨ ERRORS π¨
dehumanize_number() will fail and no number will be stored in result if:
[EINVAL]β the string instrwas empty or carried an unknown suffix. bestie what even is that suffix.[ERANGE]β the string instrrepresented a number that does not fit inresult. 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
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- nixpkgs unstable: libbsd 0.12.2
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1999, 2002, 2008 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms