1humanize_number(3bsd) LOCAL humanize_number(3bsd)
2
4 dehumanize_number, humanize_number — format a number into a human read‐
5 able form and viceversa
6
8 Utility functions from BSD systems (libbsd, -lbsd)
9
11 #include <stdlib.h>
12 (See libbsd(7) for include usage.)
13
14 int
15 dehumanize_number(const char *str, int64_t *result);
16
17 int
18 humanize_number(char *buf, size_t len, int64_t number,
19 const char *suffix, int scale, int flags);
20
22 The humanize_number() function formats the signed 64 bit quantity given
23 in number into buffer. A space and then suffix is appended to the end.
24 buffer must be at least len bytes long.
25
26 If the formatted number (including suffix) would be too long to fit into
27 buffer, then divide number by 1024 until it will. In this case, prefix
28 suffix with the appropriate SI designator.
29
30 The prefixes are:
31
32 Prefix Description Multiplier
33 k kilo 1024
34 M mega 1048576
35 G giga 1073741824
36 T tera 1099511627776
37 P peta 1125899906842624
38 E exa 1152921504606846976
39
40 len must be at least 4 plus the length of suffix, in order to ensure a
41 useful result is generated into buffer. To use a specific prefix, spec‐
42 ify this as scale (Multiplier = 1024 ^ scale). This can not be combined
43 with any of the scale flags below.
44
45 The following flags may be passed in scale:
46
47 HN_AUTOSCALE Format the buffer using the lowest multiplier possi‐
48 ble.
49
50 HN_GETSCALE Return the prefix index number (the number of times
51 number must be divided to fit) instead of formatting
52 it to the buffer.
53
54 The following flags may be passed in flags:
55
56 HN_DECIMAL If the final result is less than 10, display it using
57 one digit.
58
59 HN_NOSPACE Do not put a space between number and the prefix.
60
61 HN_B Use 'B' (bytes) as prefix if the original result does
62 not have a prefix.
63
64 HN_DIVISOR_1000
65 Divide number with 1000 instead of 1024.
66
67 The dehumanize_number() function parses the string representing an inte‐
68 gral value given in str and stores the numerical value in the integer
69 pointed to by result. The provided string may hold one of the suffixes,
70 which will be interpreted and used to scale up its accompanying numerical
71 value.
72
74 humanize_number() returns the number of characters stored in buffer (ex‐
75 cluding the terminating NUL) upon success, or -1 upon failure. If
76 HN_GETSCALE is specified, the prefix index number will be returned in‐
77 stead.
78
79 dehumanize_number() returns 0 if the string was parsed correctly. A -1
80 is returned to indicate failure and an error code is stored in errno.
81
83 dehumanize_number() will fail and no number will be stored in result if:
84
85 [EINVAL] The string in str was empty or carried an unknown suf‐
86 fix.
87
88 [ERANGE] The string in str represented a number that does not
89 fit in result.
90
92 humanize_number(9)
93
95 humanize_number() first appeared in NetBSD 2.0.
96
97 dehumanize_number() first appeared in NetBSD 5.0.
98
99BSD February 9, 2008 BSD