1HUMANIZE_NUMBER(3)       BSD Library Functions Manual       HUMANIZE_NUMBER(3)
2

NAME

4     dehumanize_number, humanize_number — format a number into a human read‐
5     able form and viceversa
6

LIBRARY

8     Utility functions from BSD systems (libbsd, -lbsd)
9

SYNOPSIS

11     #include <stdlib.h>
12
13     int
14     dehumanize_number(const char *str, int64_t *result);
15
16     int
17     humanize_number(char *buf, size_t len, int64_t number,
18         const char *suffix, int scale, int flags);
19

DESCRIPTION

21     The humanize_number() function formats the signed 64 bit quantity given
22     in number into buffer.  A space and then suffix is appended to the end.
23     buffer must be at least len bytes long.
24
25     If the formatted number (including suffix) would be too long to fit into
26     buffer, then divide number by 1024 until it will.  In this case, prefix
27     suffix with the appropriate SI designator.
28
29     The prefixes are:
30
31           Prefix    Description    Multiplier
32           k         kilo           1024
33           M         mega           1048576
34           G         giga           1073741824
35           T         tera           1099511627776
36           P         peta           1125899906842624
37           E         exa            1152921504606846976
38
39     len must be at least 4 plus the length of suffix, in order to ensure a
40     useful result is generated into buffer.  To use a specific prefix, spec‐
41     ify this as scale (Multiplier = 1024 ^ scale).  This can not be combined
42     with any of the scale flags below.
43
44     The following flags may be passed in scale:
45
46           HN_AUTOSCALE  Format the buffer using the lowest multiplier possi‐
47                         ble.
48
49           HN_GETSCALE   Return the prefix index number (the number of times
50                         number must be divided to fit) instead of formatting
51                         it to the buffer.
52
53     The following flags may be passed in flags:
54
55           HN_DECIMAL    If the final result is less than 10, display it using
56                         one digit.
57
58           HN_NOSPACE    Do not put a space between number and the prefix.
59
60           HN_B          Use 'B' (bytes) as prefix if the original result does
61                         not have a prefix.
62
63           HN_DIVISOR_1000
64                         Divide number with 1000 instead of 1024.
65
66     The dehumanize_number() function parses the string representing an inte‐
67     gral value given in str and stores the numerical value in the integer
68     pointed to by result.  The provided string may hold one of the suffixes,
69     which will be interpreted and used to scale up its accompanying numerical
70     value.
71

RETURN VALUES

73     humanize_number() returns the number of characters stored in buffer
74     (excluding the terminating NUL) upon success, or -1 upon failure.  If
75     HN_GETSCALE is specified, the prefix index number will be returned
76     instead.
77
78     dehumanize_number() returns 0 if the string was parsed correctly.  A -1
79     is returned to indicate failure and an error code is stored in errno.
80

ERRORS

82     dehumanize_number() will fail and no number will be stored in result if:
83
84     [EINVAL]           The string in str was empty or carried an unknown suf‐
85                        fix.
86
87     [ERANGE]           The string in str represented a number that does not
88                        fit in result.
89

SEE ALSO

91     humanize_number(9)
92

HISTORY

94     humanize_number() first appeared in NetBSD 2.0.
95
96     dehumanize_number() first appeared in NetBSD 5.0.
97
98BSD                            February 9, 2008                            BSD
Impressum