xref: /freebsd/lib/libutil/humanize_number.3 (revision 61881b52838a3466cdfd660cd31df12187d6fe60)
123d1a178SPawel Jakub Dawidek.\"	$NetBSD: humanize_number.3,v 1.4 2003/04/16 13:34:37 wiz Exp $
223d1a178SPawel Jakub Dawidek.\" $FreeBSD$
323d1a178SPawel Jakub Dawidek.\"
423d1a178SPawel Jakub Dawidek.\" Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
523d1a178SPawel Jakub Dawidek.\" All rights reserved.
623d1a178SPawel Jakub Dawidek.\"
723d1a178SPawel Jakub Dawidek.\" This code is derived from software contributed to The NetBSD Foundation
823d1a178SPawel Jakub Dawidek.\" by Luke Mewburn and by Tomas Svensson.
923d1a178SPawel Jakub Dawidek.\"
1023d1a178SPawel Jakub Dawidek.\" Redistribution and use in source and binary forms, with or without
1123d1a178SPawel Jakub Dawidek.\" modification, are permitted provided that the following conditions
1223d1a178SPawel Jakub Dawidek.\" are met:
1323d1a178SPawel Jakub Dawidek.\" 1. Redistributions of source code must retain the above copyright
1423d1a178SPawel Jakub Dawidek.\"    notice, this list of conditions and the following disclaimer.
1523d1a178SPawel Jakub Dawidek.\" 2. Redistributions in binary form must reproduce the above copyright
1623d1a178SPawel Jakub Dawidek.\"    notice, this list of conditions and the following disclaimer in the
1723d1a178SPawel Jakub Dawidek.\"    documentation and/or other materials provided with the distribution.
1823d1a178SPawel Jakub Dawidek.\" 3. All advertising materials mentioning features or use of this software
1923d1a178SPawel Jakub Dawidek.\"    must display the following acknowledgement:
2023d1a178SPawel Jakub Dawidek.\"        This product includes software developed by the NetBSD
2123d1a178SPawel Jakub Dawidek.\"        Foundation, Inc. and its contributors.
2223d1a178SPawel Jakub Dawidek.\" 4. Neither the name of The NetBSD Foundation nor the names of its
2323d1a178SPawel Jakub Dawidek.\"    contributors may be used to endorse or promote products derived
2423d1a178SPawel Jakub Dawidek.\"    from this software without specific prior written permission.
2523d1a178SPawel Jakub Dawidek.\"
2623d1a178SPawel Jakub Dawidek.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2723d1a178SPawel Jakub Dawidek.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2823d1a178SPawel Jakub Dawidek.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2923d1a178SPawel Jakub Dawidek.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3023d1a178SPawel Jakub Dawidek.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3123d1a178SPawel Jakub Dawidek.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3223d1a178SPawel Jakub Dawidek.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3323d1a178SPawel Jakub Dawidek.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3423d1a178SPawel Jakub Dawidek.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3523d1a178SPawel Jakub Dawidek.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3623d1a178SPawel Jakub Dawidek.\" POSSIBILITY OF SUCH DAMAGE.
3723d1a178SPawel Jakub Dawidek.\"
3869560edcSPawel Jakub Dawidek.Dd May 25, 2004
3923d1a178SPawel Jakub Dawidek.Dt HUMANIZE_NUMBER 3
4023d1a178SPawel Jakub Dawidek.Os
4123d1a178SPawel Jakub Dawidek.Sh NAME
4223d1a178SPawel Jakub Dawidek.Nm humanize_number
4323d1a178SPawel Jakub Dawidek.Nd format a number into a human readable form
44e27d1913SPawel Jakub Dawidek.Sh LIBRARY
45e27d1913SPawel Jakub Dawidek.Lb libutil
4623d1a178SPawel Jakub Dawidek.Sh SYNOPSIS
47d268591bSTom Rhodes.In libutil.h
4823d1a178SPawel Jakub Dawidek.Ft int
49fd132368SRuslan Ermilov.Fo humanize_number
50fd132368SRuslan Ermilov.Fa "char *buf" "size_t len" "int64_t number" "const char *suffix"
51fd132368SRuslan Ermilov.Fa "int scale" "int flags"
52fd132368SRuslan Ermilov.Fc
5323d1a178SPawel Jakub Dawidek.Sh DESCRIPTION
5423d1a178SPawel Jakub DawidekThe
5523d1a178SPawel Jakub Dawidek.Fn humanize_number
5669560edcSPawel Jakub Dawidekfunction formats the signed 64-bit quantity given in
5723d1a178SPawel Jakub Dawidek.Fa number
5823d1a178SPawel Jakub Dawidekinto
5923d1a178SPawel Jakub Dawidek.Fa buffer .
6023d1a178SPawel Jakub DawidekA space and then
6123d1a178SPawel Jakub Dawidek.Fa suffix
6223d1a178SPawel Jakub Dawidekis appended to the end.
6369560edcSPawel Jakub DawidekThe buffer pointed to by
6423d1a178SPawel Jakub Dawidek.Fa buffer
6523d1a178SPawel Jakub Dawidekmust be at least
6623d1a178SPawel Jakub Dawidek.Fa len
6713c273c8SXin LIbytes long.
6823d1a178SPawel Jakub Dawidek.Pp
6923d1a178SPawel Jakub DawidekIf the formatted number (including
7023d1a178SPawel Jakub Dawidek.Fa suffix )
7123d1a178SPawel Jakub Dawidekwould be too long to fit into
7223d1a178SPawel Jakub Dawidek.Fa buffer ,
7323d1a178SPawel Jakub Dawidekthen divide
7423d1a178SPawel Jakub Dawidek.Fa number
7523d1a178SPawel Jakub Dawidekby 1024 until it will.
7623d1a178SPawel Jakub DawidekIn this case, prefix
7723d1a178SPawel Jakub Dawidek.Fa suffix
7823d1a178SPawel Jakub Dawidekwith the appropriate SI designator.
79a73a3ab5SRuslan ErmilovThe
80a73a3ab5SRuslan Ermilov.Fn humanize_number
81a73a3ab5SRuslan Ermilovfunction
82d5fbc8f4SWarner Loshfollows the traditional computer science conventions rather than the proposed
83d5fbc8f4SWarner LoshSI power of two convention.
8423d1a178SPawel Jakub Dawidek.Pp
8523d1a178SPawel Jakub DawidekThe prefixes are:
86d5fbc8f4SWarner Losh.Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
87d5fbc8f4SWarner Losh.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier" Ta Sy "Multiplier 1000x"
88d5fbc8f4SWarner Losh.It Li k Ta No kilo Ta 1024 Ta 1000
89d5fbc8f4SWarner Losh.It Li M Ta No mega Ta 1048576 Ta 1000000
90d5fbc8f4SWarner Losh.It Li G Ta No giga Ta 1073741824 Ta 1000000000
91d5fbc8f4SWarner Losh.It Li T Ta No tera Ta 1099511627776 Ta 1000000000000
92d5fbc8f4SWarner Losh.It Li P Ta No peta Ta 1125899906842624 Ta 1000000000000000
93d5fbc8f4SWarner Losh.It Li E Ta No exa Ta 1152921504606846976 Ta 1000000000000000000
9423d1a178SPawel Jakub Dawidek.El
9523d1a178SPawel Jakub Dawidek.Pp
9669560edcSPawel Jakub DawidekThe
9723d1a178SPawel Jakub Dawidek.Fa len
9869560edcSPawel Jakub Dawidekargument must be at least 4 plus the length of
9923d1a178SPawel Jakub Dawidek.Fa suffix ,
10023d1a178SPawel Jakub Dawidekin order to ensure a useful result is generated into
10123d1a178SPawel Jakub Dawidek.Fa buffer .
10223d1a178SPawel Jakub DawidekTo use a specific prefix, specify this as
10323d1a178SPawel Jakub Dawidek.Fa scale
10469560edcSPawel Jakub Dawidek(multiplier = 1024 ^ scale).
10523d1a178SPawel Jakub DawidekThis cannot be combined with any of the
10623d1a178SPawel Jakub Dawidek.Fa scale
10723d1a178SPawel Jakub Dawidekflags below.
10823d1a178SPawel Jakub Dawidek.Pp
10923d1a178SPawel Jakub DawidekThe following flags may be passed in
11069560edcSPawel Jakub Dawidek.Fa scale :
11169560edcSPawel Jakub Dawidek.Bl -tag -width ".Dv HN_DIVISOR_1000" -offset indent
11223d1a178SPawel Jakub Dawidek.It Dv HN_AUTOSCALE
11323d1a178SPawel Jakub DawidekFormat the buffer using the lowest multiplier possible.
11423d1a178SPawel Jakub Dawidek.It Dv HN_GETSCALE
11523d1a178SPawel Jakub DawidekReturn the prefix index number (the number of times
11623d1a178SPawel Jakub Dawidek.Fa number
11723d1a178SPawel Jakub Dawidekmust be divided to fit) instead of formatting it to the buffer.
11823d1a178SPawel Jakub Dawidek.El
11923d1a178SPawel Jakub Dawidek.Pp
12023d1a178SPawel Jakub DawidekThe following flags may be passed in
12169560edcSPawel Jakub Dawidek.Fa flags :
12269560edcSPawel Jakub Dawidek.Bl -tag -width ".Dv HN_DIVISOR_1000" -offset indent
12323d1a178SPawel Jakub Dawidek.It Dv HN_DECIMAL
12423d1a178SPawel Jakub DawidekIf the final result is less than 10, display it using one digit.
12523d1a178SPawel Jakub Dawidek.It Dv HN_NOSPACE
12623d1a178SPawel Jakub DawidekDo not put a space between
12723d1a178SPawel Jakub Dawidek.Fa number
12823d1a178SPawel Jakub Dawidekand the prefix.
12923d1a178SPawel Jakub Dawidek.It Dv HN_B
13069560edcSPawel Jakub DawidekUse
13169560edcSPawel Jakub Dawidek.Ql B
13269560edcSPawel Jakub Dawidek(bytes) as prefix if the original result does not have a prefix.
13323d1a178SPawel Jakub Dawidek.It Dv HN_DIVISOR_1000
13423d1a178SPawel Jakub DawidekDivide
13523d1a178SPawel Jakub Dawidek.Fa number
13623d1a178SPawel Jakub Dawidekwith 1000 instead of 1024.
13723d1a178SPawel Jakub Dawidek.El
13823d1a178SPawel Jakub Dawidek.Sh RETURN VALUES
13969560edcSPawel Jakub DawidekThe
14023d1a178SPawel Jakub Dawidek.Fn humanize_number
14169560edcSPawel Jakub Dawidekfunction returns the number of characters stored in
14223d1a178SPawel Jakub Dawidek.Fa buffer
14369560edcSPawel Jakub Dawidek(excluding the terminating
14469560edcSPawel Jakub Dawidek.Dv NUL )
14569560edcSPawel Jakub Dawidekupon success, or \-1 upon failure.
14623d1a178SPawel Jakub DawidekIf
14723d1a178SPawel Jakub Dawidek.Dv HN_GETSCALE
14823d1a178SPawel Jakub Dawidekis specified, the prefix index number will be returned instead.
149c2fc8cebSPawel Jakub Dawidek.Sh SEE ALSO
150c2fc8cebSPawel Jakub Dawidek.Xr expand_number 3
15123d1a178SPawel Jakub Dawidek.Sh HISTORY
15269560edcSPawel Jakub DawidekThe
15323d1a178SPawel Jakub Dawidek.Fn humanize_number
15469560edcSPawel Jakub Dawidekfunction first appeared in
15561881b52SDavid E. O'Brien.Nx 2.0
15661881b52SDavid E. O'Brienand then in
15761881b52SDavid E. O'Brien.Fx 5.3 .
158