xref: /freebsd/lib/libutil/humanize_number.3 (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1.\"	$NetBSD: humanize_number.3,v 1.4 2003/04/16 13:34:37 wiz Exp $
2.\" $FreeBSD$
3.\"
4.\" Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Luke Mewburn and by Tomas Svensson.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"        This product includes software developed by the NetBSD
21.\"        Foundation, Inc. and its contributors.
22.\" 4. Neither the name of The NetBSD Foundation nor the names of its
23.\"    contributors may be used to endorse or promote products derived
24.\"    from this software without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36.\" POSSIBILITY OF SUCH DAMAGE.
37.\"
38.Dd May 25, 2004
39.Dt HUMANIZE_NUMBER 3
40.Os
41.Sh NAME
42.Nm humanize_number
43.Nd format a number into a human readable form
44.Sh LIBRARY
45.Lb libutil
46.Sh SYNOPSIS
47.In libutil.h
48.Ft int
49.Fn humanize_number "char *buf" "size_t len" "int64_t number" "const char *suffix" "int scale" "int flags"
50.Sh DESCRIPTION
51The
52.Fn humanize_number
53function formats the signed 64 bit quantity given in
54.Fa number
55into
56.Fa buffer .
57A space and then
58.Fa suffix
59is appended to the end.
60.Fa buffer
61must be at least
62.Fa len
63bytes bytes long.
64.Pp
65If the formatted number (including
66.Fa suffix )
67would be too long to fit into
68.Fa buffer ,
69then divide
70.Fa number
71by 1024 until it will.
72In this case, prefix
73.Fa suffix
74with the appropriate SI designator.
75.Pp
76The prefixes are:
77.Bl -column "Prefix" "Description" "Multiplier" -offset indent
78.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
79.It k	kilo	1024
80.It M	mega	1048576
81.It G	giga	1073741824
82.It T	tera	1099511627776
83.It P	peta	1125899906842624
84.It E	exa	1152921504606846976
85.El
86.Pp
87The
88.Fa len
89argument must be at least 4 plus the length of
90.Fa suffix ,
91in order to ensure a useful result is generated into
92.Fa buffer .
93To use a specific prefix, specify this as
94.Fa scale
95(Multiplier = 1024 ^ scale).
96This can not be combined with any of the
97.Fa scale
98flags below.
99.Pp
100The following flags may be passed in
101.Pa scale :
102.Bl -tag -width Dv -offset indent
103.It Dv HN_AUTOSCALE
104Format the buffer using the lowest multiplier possible.
105.It Dv HN_GETSCALE
106Return the prefix index number (the number of times
107.Fa number
108must be divided to fit) instead of formatting it to the buffer.
109.El
110.Pp
111The following flags may be passed in
112.Pa flags :
113.Bl -tag -width Dv -offset indent
114.It Dv HN_DECIMAL
115If the final result is less than 10, display it using one digit.
116.It Dv HN_NOSPACE
117Do not put a space between
118.Fa number
119and the prefix.
120.It Dv HN_B
121Use 'B' (bytes) as prefix if the original result does not have a prefix.
122.It Dv HN_DIVISOR_1000
123Divide
124.Fa number
125with 1000 instead of 1024.
126.El
127.Sh RETURN VALUES
128The
129.Fn humanize_number
130function returns the number of characters stored in
131.Fa buffer
132(excluding the terminating NUL) upon success, or \-1 upon failure.
133If
134.Dv HN_GETSCALE
135is specified, the prefix index number will be returned instead.
136.Sh SEE ALSO
137.Xr humanize_number 9
138.Sh HISTORY
139The
140.Fn humanize_number
141function first appeared in
142.Nx 2.0 .
143