humanize_number
- format a number into a human readable form
LIBRARY
Lb libutil
SYNOPSIS
#include <libutil.h> int
Fo humanize_number
Fa char *buf size_t len int64_t number const char *suffix
Fa int scale int flags
Fc
DESCRIPTION
The
humanize_number ();
function formats the signed 64-bit quantity given in
Fa number
into
Fa buffer .
A space and then
Fa suffix
is appended to the end.
The buffer pointed to by
Fa buffer
must be at least
Fa len
bytes long.
If the formatted number (including
Fa suffix )
would be too long to fit into
Fa buffer ,
then divide
Fa number
by 1024 until it will.
In this case, prefix
Fa suffix
with the appropriate SI designator.
The
humanize_number ();
function
follows the traditional computer science conventions rather than the proposed
SI power of two convention.
The prefixes are:
Prefix Ta Description Ta Multiplier Ta Multiplier 1000x
k Ta kilo Ta 1024 Ta 1000
M Ta mega Ta 1048576 Ta 1000000
G Ta giga Ta 1073741824 Ta 1000000000
T Ta tera Ta 1099511627776 Ta 1000000000000
P Ta peta Ta 1125899906842624 Ta 1000000000000000
E Ta exa Ta 1152921504606846976 Ta 1000000000000000000
The
Fa len
argument must be at least 4 plus the length of
Fa suffix ,
in order to ensure a useful result is generated into
Fa buffer .
To use a specific prefix, specify this as
Fa scale
(multiplier = 1024 ^ scale).
This cannot be combined with any of the
Fa scale
flags below.
The following flags may be passed in
Fa scale :
HN_AUTOSCALE
Format the buffer using the lowest multiplier possible.
HN_GETSCALE
Return the prefix index number (the number of times
Fa number
must be divided to fit) instead of formatting it to the buffer.
The following flags may be passed in
Fa flags :
HN_DECIMAL
If the final result is less than 10, display it using one digit.
HN_NOSPACE
Do not put a space between
Fa number
and the prefix.
HN_B
Use
`B'
(bytes) as prefix if the original result does not have a prefix.
HN_DIVISOR_1000
Divide
Fa number
with 1000 instead of 1024.
RETURN VALUES
The
humanize_number ();
function returns the number of characters stored in
Fa buffer
(excluding the terminating
NUL
upon success, or -1 upon failure.
If
HN_GETSCALE
is specified, the prefix index number will be returned instead.
SEE ALSO
expand_number3
HISTORY
The
humanize_number ();
function first appeared in
Nx 2.0
and then in
Fx 5.3 .