xref: /netbsd-src/lib/libc/gen/humanize_number.3 (revision 928749aba22fc1530555c9f4f778b1746cf71ffa)
1.\"	$NetBSD: humanize_number.3,v 1.13 2019/03/12 22:21:53 wiz Exp $
2.\"
3.\" Copyright (c) 1999, 2002, 2008 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Luke Mewburn and by Tomas Svensson.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd March 11, 2019
31.Dt HUMANIZE_NUMBER 3
32.Os
33.Sh NAME
34.Nm dehumanize_number ,
35.Nm humanize_number
36.Nd format a number into a human readable form and vice versa
37.Sh SYNOPSIS
38.In stdlib.h
39.Ft int
40.Fn dehumanize_number "const char *str" "int64_t *result"
41.Ft int
42.Fn humanize_number "char *buffer" "size_t len" "int64_t number" "const char *suffix" "int scale" "int flags"
43.Sh DESCRIPTION
44The
45.Fn humanize_number
46function formats the signed 64 bit quantity given in
47.Fa number
48into
49.Fa buffer .
50A space and then the
51.Fa suffix
52.Pq "if not null"
53is appended to the end.
54.Fa len
55gives the size of the
56.Fa buffer .
57.Pp
58If the formatted number (including
59.Fa suffix )
60would be too long to fit into
61.Fa buffer ,
62then repeatedly divide
63.Fa number
64by 1024 until it will fit.
65In this case, prefix
66.Fa suffix
67with the appropriate SI designator.
68.Pp
69The prefixes are:
70.Bl -column "Prefix" "Description" "Multiplier" -offset indent
71.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
72.It k	kilo	1024
73.It M	mega	1048576
74.It G	giga	1073741824
75.It T	tera	1099511627776
76.It P	peta	1125899906842624
77.It E	exa	1152921504606846976
78.El
79.Pp
80.Fa len
81must be at least 4 plus the length of
82.Fa suffix ,
83in order to ensure a useful result is generated into
84.Fa buffer .
85To use a specific prefix, specify this as
86.Fa scale
87(Multiplier = 1024 ^ scale).
88The
89.Fa scale
90must be at least 0 and no more than 6.
91.Pp
92Alternatively, one of the following special values may be given as
93.Pa scale :
94.Bl -tag -width Dv -offset indent
95.It Dv HN_AUTOSCALE
96Format the buffer using the lowest multiplier possible.
97.It Dv HN_GETSCALE
98Return the prefix index number (the number of times
99.Fa number
100must be divided to fit) instead of formatting it to the buffer.
101That is, the
102.Fa scale
103that would have been used if
104.Dv HN_AUTOSCALE
105had been used.
106.El
107.Pp
108The following flags may be passed in
109.Pa flags :
110.Bl -tag -width Dv -offset indent
111.It Dv HN_DECIMAL
112If the final numeric result is less than 10,
113and is not the same as the original value (that is, it has been scaled)
114display it using a decimal radix character, and one following digit.
115.It Dv HN_NOSPACE
116Do not put a space between
117.Fa number
118and the prefix.
119.It Dv HN_B
120Use 'B' (bytes) as prefix if the original result does not have a prefix.
121.It Dv HN_DIVISOR_1000
122Divide
123.Fa number
124with 1000 instead of 1024.
125That is, use decimal scaling instead of binary.
126.El
127.Pp
128To generate the shortest meaningful value,
129a buffer length
130.Pq Fa len
131that is 6 greater the length of the
132.Fa suffix
133along with
134.Dv HN_AUTOSCALE
135will ensure the highest meaningful scale is used.
136Allow one extra byte for the sign if the number is negative,
137and one less if the
138.Dv HN_NOSPACE
139flag is used.
140.Pp
141The
142.Fn dehumanize_number
143function parses the string representing an integral value given in
144.Fa str
145and stores the numerical value in the integer pointed to by
146.Fa result .
147The provided string may hold one of the suffixes, which will be interpreted
148and used to scale up its accompanying numerical value.
149.Sh RETURN VALUES
150.Fn humanize_number
151returns the number of characters stored in
152.Fa buffer
153(excluding the terminating NUL) upon success, or \-1 upon failure.
154If
155.Dv HN_GETSCALE
156is specified, the prefix index number will be returned instead.
157.Pp
158.Fn dehumanize_number
159returns 0 if the string was parsed correctly.
160A \-1 is returned to indicate failure and an error code is stored in
161.Va errno .
162.Sh ERRORS
163.Fn dehumanize_number
164will fail and no number will be stored in
165.Fa result
166if:
167.Bl -tag -width Er
168.It Bq Er EINVAL
169The string in
170.Fa str
171was empty or carried an unknown suffix.
172.It Bq Er ERANGE
173The string in
174.Fa str
175represented a number that does not fit in
176.Fa result .
177.El
178.Sh SEE ALSO
179.Xr strsuftoll 3 ,
180.Xr orders 7 ,
181.Xr humanize_number 9
182.Sh HISTORY
183.Fn humanize_number
184first appeared in
185.Nx 2.0 .
186.Pp
187.Fn dehumanize_number
188first appeared in
189.Nx 5.0 .
190