xref: /f-stack/tools/libutil/humanize_number.3 (revision 22ce4aff)
11eaf0ac3Slogwang.\"	$NetBSD: humanize_number.3,v 1.4 2003/04/16 13:34:37 wiz Exp $
21eaf0ac3Slogwang.\" $FreeBSD$
31eaf0ac3Slogwang.\"
41eaf0ac3Slogwang.\" Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
51eaf0ac3Slogwang.\" All rights reserved.
61eaf0ac3Slogwang.\"
71eaf0ac3Slogwang.\" This code is derived from software contributed to The NetBSD Foundation
81eaf0ac3Slogwang.\" by Luke Mewburn and by Tomas Svensson.
91eaf0ac3Slogwang.\"
101eaf0ac3Slogwang.\" Redistribution and use in source and binary forms, with or without
111eaf0ac3Slogwang.\" modification, are permitted provided that the following conditions
121eaf0ac3Slogwang.\" are met:
131eaf0ac3Slogwang.\" 1. Redistributions of source code must retain the above copyright
141eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer.
151eaf0ac3Slogwang.\" 2. Redistributions in binary form must reproduce the above copyright
161eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer in the
171eaf0ac3Slogwang.\"    documentation and/or other materials provided with the distribution.
181eaf0ac3Slogwang.\"
191eaf0ac3Slogwang.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201eaf0ac3Slogwang.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211eaf0ac3Slogwang.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221eaf0ac3Slogwang.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231eaf0ac3Slogwang.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241eaf0ac3Slogwang.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251eaf0ac3Slogwang.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261eaf0ac3Slogwang.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271eaf0ac3Slogwang.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281eaf0ac3Slogwang.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291eaf0ac3Slogwang.\" POSSIBILITY OF SUCH DAMAGE.
301eaf0ac3Slogwang.\"
311eaf0ac3Slogwang.Dd October 7, 2013
321eaf0ac3Slogwang.Dt HUMANIZE_NUMBER 3
331eaf0ac3Slogwang.Os
341eaf0ac3Slogwang.Sh NAME
351eaf0ac3Slogwang.Nm humanize_number
361eaf0ac3Slogwang.Nd format a number into a human readable form
371eaf0ac3Slogwang.Sh LIBRARY
381eaf0ac3Slogwang.Lb libutil
391eaf0ac3Slogwang.Sh SYNOPSIS
401eaf0ac3Slogwang.In libutil.h
411eaf0ac3Slogwang.Ft int
421eaf0ac3Slogwang.Fo humanize_number
431eaf0ac3Slogwang.Fa "char *buf" "size_t len" "int64_t number" "const char *suffix"
441eaf0ac3Slogwang.Fa "int scale" "int flags"
451eaf0ac3Slogwang.Fc
461eaf0ac3Slogwang.Sh DESCRIPTION
471eaf0ac3SlogwangThe
481eaf0ac3Slogwang.Fn humanize_number
491eaf0ac3Slogwangfunction formats the signed 64-bit quantity given in
501eaf0ac3Slogwang.Fa number
511eaf0ac3Slogwanginto
521eaf0ac3Slogwang.Fa buf .
531eaf0ac3SlogwangA space and then
541eaf0ac3Slogwang.Fa suffix
551eaf0ac3Slogwangis appended to the end.
561eaf0ac3SlogwangThe buffer pointed to by
571eaf0ac3Slogwang.Fa buf
581eaf0ac3Slogwangmust be at least
591eaf0ac3Slogwang.Fa len
601eaf0ac3Slogwangbytes long.
611eaf0ac3Slogwang.Pp
621eaf0ac3SlogwangIf the formatted number (including
631eaf0ac3Slogwang.Fa suffix )
641eaf0ac3Slogwangwould be too long to fit into
651eaf0ac3Slogwang.Fa buf ,
661eaf0ac3Slogwangthen divide
671eaf0ac3Slogwang.Fa number
681eaf0ac3Slogwangby 1024 until it will.
691eaf0ac3SlogwangIn this case, prefix
701eaf0ac3Slogwang.Fa suffix
711eaf0ac3Slogwangwith the appropriate designator.
721eaf0ac3SlogwangThe
731eaf0ac3Slogwang.Fn humanize_number
741eaf0ac3Slogwangfunction follows the traditional computer science conventions by
751eaf0ac3Slogwangdefault, rather than the IEE/IEC (and now also SI) power of two
761eaf0ac3Slogwangconvention or the power of ten notion.
771eaf0ac3SlogwangThis behaviour however can be altered by specifying the
781eaf0ac3Slogwang.Dv HN_DIVISOR_1000
791eaf0ac3Slogwangand
801eaf0ac3Slogwang.Dv HN_IEC_PREFIXES
811eaf0ac3Slogwangflags.
821eaf0ac3Slogwang.Pp
831eaf0ac3SlogwangThe traditional
841eaf0ac3Slogwang.Pq default
851eaf0ac3Slogwangprefixes are:
861eaf0ac3Slogwang.Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
871eaf0ac3Slogwang.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier" Ta Sy "Multiplier 1000x"
881eaf0ac3Slogwang.It Li (note) Ta No kilo Ta 1024 Ta 1000
891eaf0ac3Slogwang.It Li M Ta No mega Ta 1048576 Ta 1000000
901eaf0ac3Slogwang.It Li G Ta No giga Ta 1073741824 Ta 1000000000
911eaf0ac3Slogwang.It Li T Ta No tera Ta 1099511627776 Ta 1000000000000
921eaf0ac3Slogwang.It Li P Ta No peta Ta 1125899906842624 Ta 1000000000000000
931eaf0ac3Slogwang.It Li E Ta No exa Ta 1152921504606846976 Ta 1000000000000000000
941eaf0ac3Slogwang.El
951eaf0ac3Slogwang.Pp
961eaf0ac3SlogwangNote:
971eaf0ac3SlogwangAn uppercase K indicates a power of two, a lowercase k a power of ten.
981eaf0ac3Slogwang.Pp
991eaf0ac3SlogwangThe IEE/IEC (and now also SI) power of two prefixes are:
1001eaf0ac3Slogwang.Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
1011eaf0ac3Slogwang.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
1021eaf0ac3Slogwang.It Li Ki Ta No kibi Ta 1024
1031eaf0ac3Slogwang.It Li Mi Ta No mebi Ta 1048576
1041eaf0ac3Slogwang.It Li Gi Ta No gibi Ta 1073741824
1051eaf0ac3Slogwang.It Li Ti Ta No tebi Ta 1099511627776
1061eaf0ac3Slogwang.It Li Pi Ta No pebi Ta 1125899906842624
1071eaf0ac3Slogwang.It Li Ei Ta No exbi Ta 1152921504606846976
1081eaf0ac3Slogwang.El
1091eaf0ac3Slogwang.Pp
1101eaf0ac3SlogwangThe
1111eaf0ac3Slogwang.Fa len
1121eaf0ac3Slogwangargument must be at least 4 plus the length of
1131eaf0ac3Slogwang.Fa suffix ,
1141eaf0ac3Slogwangin order to ensure a useful result is generated into
1151eaf0ac3Slogwang.Fa buf .
1161eaf0ac3SlogwangTo use a specific prefix, specify this as
1171eaf0ac3Slogwang.Fa scale
1181eaf0ac3Slogwang.Po multiplier = 1024 ^ scale;
1191eaf0ac3Slogwangwhen
1201eaf0ac3Slogwang.Dv HN_DIVISOR_1000
1211eaf0ac3Slogwangis specified,
1221eaf0ac3Slogwangmultiplier = 1000 ^ scale
1231eaf0ac3Slogwang.Pc .
1241eaf0ac3SlogwangThis cannot be combined with any of the
1251eaf0ac3Slogwang.Fa scale
1261eaf0ac3Slogwangflags below.
1271eaf0ac3Slogwang.Pp
1281eaf0ac3SlogwangThe following flags may be passed in
1291eaf0ac3Slogwang.Fa scale :
1301eaf0ac3Slogwang.Bl -tag -width ".Dv HN_DIVISOR_1000" -offset indent
1311eaf0ac3Slogwang.It Dv HN_AUTOSCALE
1321eaf0ac3SlogwangFormat the buffer using the lowest multiplier possible.
1331eaf0ac3Slogwang.It Dv HN_GETSCALE
1341eaf0ac3SlogwangReturn the prefix index number (the number of times
1351eaf0ac3Slogwang.Fa number
1361eaf0ac3Slogwangmust be divided to fit) instead of formatting it to the buffer.
1371eaf0ac3Slogwang.El
1381eaf0ac3Slogwang.Pp
1391eaf0ac3SlogwangThe following flags may be passed in
1401eaf0ac3Slogwang.Fa flags :
1411eaf0ac3Slogwang.Bl -tag -width ".Dv HN_DIVISOR_1000" -offset indent
1421eaf0ac3Slogwang.It Dv HN_DECIMAL
1431eaf0ac3SlogwangIf the final result is less than 10, display it using one decimal place.
1441eaf0ac3Slogwang.It Dv HN_NOSPACE
1451eaf0ac3SlogwangDo not put a space between
1461eaf0ac3Slogwang.Fa number
1471eaf0ac3Slogwangand the prefix.
1481eaf0ac3Slogwang.It Dv HN_B
1491eaf0ac3SlogwangUse
1501eaf0ac3Slogwang.Ql B
1511eaf0ac3Slogwang(bytes) as prefix if the original result does not have a prefix.
1521eaf0ac3Slogwang.It Dv HN_DIVISOR_1000
1531eaf0ac3SlogwangDivide
1541eaf0ac3Slogwang.Fa number
1551eaf0ac3Slogwangwith 1000 instead of 1024.
1561eaf0ac3Slogwang.It Dv HN_IEC_PREFIXES
1571eaf0ac3SlogwangUse the IEE/IEC notion of prefixes (Ki, Mi, Gi...).
1581eaf0ac3SlogwangThis flag has no effect when
1591eaf0ac3Slogwang.Dv HN_DIVISOR_1000
1601eaf0ac3Slogwangis also specified.
1611eaf0ac3Slogwang.El
1621eaf0ac3Slogwang.Sh RETURN VALUES
1631eaf0ac3SlogwangUpon success, the
1641eaf0ac3Slogwang.Nm
1651eaf0ac3Slogwangfunction returns the number of characters that would have been stored in
1661eaf0ac3Slogwang.Fa buf
1671eaf0ac3Slogwang(excluding the terminating
1681eaf0ac3Slogwang.Dv NUL )
1691eaf0ac3Slogwangif
1701eaf0ac3Slogwang.Fa buf
1711eaf0ac3Slogwangwas large enough, or \-1 upon failure.
1721eaf0ac3SlogwangEven upon failure, the contents of
1731eaf0ac3Slogwang.Fa buf
1741eaf0ac3Slogwangmay be modified.
1751eaf0ac3SlogwangIf
1761eaf0ac3Slogwang.Dv HN_GETSCALE
1771eaf0ac3Slogwangis specified, the prefix index number will be returned instead.
1781eaf0ac3Slogwang.Sh SEE ALSO
1791eaf0ac3Slogwang.Xr expand_number 3
1801eaf0ac3Slogwang.Sh STANDARDS
1811eaf0ac3SlogwangThe
1821eaf0ac3Slogwang.Dv HN_DIVISOR_1000
1831eaf0ac3Slogwangand
1841eaf0ac3Slogwang.Dv HN_IEC_PREFIXES
1851eaf0ac3Slogwangflags
1861eaf0ac3Slogwangconform to
1871eaf0ac3Slogwang.Tn ISO/IEC
1881eaf0ac3SlogwangStd\~80000-13:2008
1891eaf0ac3Slogwangand
1901eaf0ac3Slogwang.Tn IEEE
1911eaf0ac3SlogwangStd\~1541-2002.
1921eaf0ac3Slogwang.Sh HISTORY
1931eaf0ac3SlogwangThe
1941eaf0ac3Slogwang.Fn humanize_number
1951eaf0ac3Slogwangfunction first appeared in
1961eaf0ac3Slogwang.Nx 2.0
1971eaf0ac3Slogwangand then in
1981eaf0ac3Slogwang.Fx 5.3 .
1991eaf0ac3SlogwangThe
2001eaf0ac3Slogwang.Dv HN_IEC_PREFIXES
2011eaf0ac3Slogwangflag was introduced in
2021eaf0ac3Slogwang.Fx 9.0 .
203*22ce4affSfengbojiang.Sh CAVEATS
204*22ce4affSfengbojiangFor numbers greater than 999 using buffer length of 4 and less can cause
205*22ce4affSfengbojiangrounding errors.
206*22ce4affSfengbojiangWhen using
207*22ce4affSfengbojiang.Dv HN_IEC_PREFIXES
208*22ce4affSfengbojiangthe same error occurs for buffer length of 5 or less.
209