1 /* $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $ */
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org>
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: head/lib/libutil/humanize_number.c 256130 2013-10-07 22:22:57Z jmg $
36 #include <sys/types.h>
45 static const int maxscale
= 7;
48 humanize_number(char *buf
, size_t len
, int64_t quotient
,
49 const char *suffix
, int scale
, int flags
)
51 const char *prefixes
, *sep
;
52 int i
, r
, remainder
, s1
, s2
, sign
;
57 /* Since so many callers don't check -1, NUL terminate the buffer */
62 if (buf
== NULL
|| suffix
== NULL
)
66 else if (scale
>= maxscale
&&
67 ((scale
& ~(HN_AUTOSCALE
|HN_GETSCALE
)) != 0))
69 if ((flags
& HN_DIVISOR_1000
) && (flags
& HN_IEC_PREFIXES
))
72 /* setup parameters */
75 if (flags
& HN_IEC_PREFIXES
) {
78 * Use the prefixes for power of two recommended by
79 * the International Electrotechnical Commission
80 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
82 * HN_IEC_PREFIXES implies a divisor of 1024 here
83 * (use of HN_DIVISOR_1000 would have triggered
84 * an assertion earlier).
87 divisordeccut
= 973; /* ceil(.95 * 1024) */
89 prefixes
= "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
91 prefixes
= "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
94 if (flags
& HN_DIVISOR_1000
) {
98 prefixes
= "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
100 prefixes
= "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
103 divisordeccut
= 973; /* ceil(.95 * 1024) */
105 prefixes
= "B\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
107 prefixes
= "\0\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
111 #define SCALE2PREFIX(scale) (&prefixes[(scale) * 3])
115 quotient
= -quotient
;
116 baselen
+= 2; /* sign, digit */
119 baselen
+= 1; /* digit */
121 if (flags
& HN_NOSPACE
)
127 baselen
+= strlen(suffix
);
129 /* Check if enough room for `x y' + suffix + `\0' */
130 if (len
< baselen
+ 1)
133 if (scale
& (HN_AUTOSCALE
| HN_GETSCALE
)) {
134 /* See if there is additional columns can be used. */
135 for (max
= 1, i
= len
- baselen
; i
-- > 0;)
139 * Divide the number until it fits the given column.
140 * If there will be an overflow by the rounding below,
144 (quotient
>= max
|| (quotient
== max
- 1 &&
145 remainder
>= divisordeccut
)) && i
< maxscale
; i
++) {
146 remainder
= quotient
% divisor
;
150 if (scale
& HN_GETSCALE
)
153 for (i
= 0; i
< scale
&& i
< maxscale
; i
++) {
154 remainder
= quotient
% divisor
;
160 * Generate base output
162 r
= snprintf(buf
, len
, "%" PRId64
"%s%s%s",
163 sign
* (quotient
+ (remainder
+ divisor
/ 2) / divisor
),
164 sep
, SCALE2PREFIX(i
), suffix
);
166 if ((flags
& HN_FRACTIONAL
) && (u_int
)r
+ 3 <= len
&& i
) {
168 * If FRACTIONAL is specified output up to two fractional
169 * digits, unless the value was not divided out.
174 n
= (int)len
- r
- 2;
176 if (n
> 2) /* max 2 fractional digits */
183 s1
= (int)quotient
+ ((remainder
* frac
+ divisor
/ 2) /
185 s2
= ((remainder
* frac
+ divisor
/ 2) / divisor
) % frac
;
186 r
= snprintf(buf
, len
, "%d%s%d%s%s%s",
187 sign
* s1
, localeconv()->decimal_point
, s2
,
188 sep
, SCALE2PREFIX(i
), suffix
);
189 } else if ((flags
& HN_DECIMAL
) && (u_int
)r
+ 3 <= len
&&
190 (((quotient
== 9 && remainder
< divisordeccut
) ||
191 quotient
< 9) && i
> 0)) {
193 * If DECIMAL is specified and the value is <= 9.9 after
194 * rounding, and it fits, add one fractional digit.
196 s1
= (int)quotient
+ ((remainder
* 10 + divisor
/ 2) /
198 s2
= ((remainder
* 10 + divisor
/ 2) / divisor
) % 10;
199 r
= snprintf(buf
, len
, "%d%s%d%s%s%s",
200 sign
* s1
, localeconv()->decimal_point
, s2
,
201 sep
, SCALE2PREFIX(i
), suffix
);