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.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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 assert(suffix != NULL);
59 assert(scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0)));
60 assert(!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES)));
64 if (flags & HN_IEC_PREFIXES) {
67 * Use the prefixes for power of two recommended by
68 * the International Electrotechnical Commission
69 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
71 * HN_IEC_PREFIXES implies a divisor of 1024 here
72 * (use of HN_DIVISOR_1000 would have triggered
73 * an assertion earlier).
77 prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
79 prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
82 if (flags & HN_DIVISOR_1000)
88 prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
90 prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
93 #define SCALE2PREFIX(scale) (&prefixes[(scale) * 3])
95 if (scale < 0 || (scale >= maxscale &&
96 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0))
99 if (buf == NULL || suffix == NULL)
106 quotient = -quotient;
107 baselen += 2; /* sign, digit */
110 baselen += 1; /* digit */
112 if (flags & HN_NOSPACE)
118 baselen += strlen(suffix);
120 /* Check if enough room for `x y' + suffix + `\0' */
121 if (len < baselen + 1)
124 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
125 /* See if there is additional columns can be used. */
126 for (max = 1, i = len - baselen; i-- > 0;)
130 * Divide the number until it fits the given column.
131 * If there will be an overflow by the rounding below,
135 (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
137 remainder = quotient % divisor;
141 if (scale & HN_GETSCALE)
144 for (i = 0; i < scale && i < maxscale; i++) {
145 remainder = quotient % divisor;
150 /* If a value <= 9.9 after rounding and ... */
151 if (quotient <= 9 && remainder < 950 && i > 0 && flags & HN_DECIMAL) {
152 /* baselen + \0 + .N */
153 if (len < baselen + 1 + 2)
155 s1 = (int)quotient + ((remainder + 50) / 1000);
156 s2 = ((remainder + 50) / 100) % 10;
157 r = snprintf(buf, len, "%d%s%d%s%s%s",
158 sign * s1, localeconv()->decimal_point, s2,
159 sep, SCALE2PREFIX(i), suffix);
161 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
162 sign * (quotient + (remainder + 50) / 1000),
163 sep, SCALE2PREFIX(i), suffix);