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>
46 humanize_number(char *buf, size_t len, int64_t bytes,
47 const char *suffix, int scale, int flags)
49 const char *prefixes, *sep;
50 int b, i, r, maxscale, s1, s2, sign;
55 assert(suffix != NULL);
58 if (flags & HN_DIVISOR_1000) {
59 /* SI for decimal multiplies */
62 prefixes = "B\0k\0M\0G\0T\0P\0E";
64 prefixes = "\0\0k\0M\0G\0T\0P\0E";
68 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
72 prefixes = "B\0K\0M\0G\0T\0P\0E";
74 prefixes = "\0\0K\0M\0G\0T\0P\0E";
77 #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
80 if (scale >= maxscale &&
81 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
84 if (buf == NULL || suffix == NULL)
92 baselen = 3; /* sign, digit, prefix */
96 baselen = 2; /* digit, prefix */
98 if (flags & HN_NOSPACE)
104 baselen += strlen(suffix);
106 /* Check if enough room for `x y' + suffix + `\0' */
107 if (len < baselen + 1)
110 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
111 /* See if there is additional columns can be used. */
112 for (max = 100, i = len - baselen; i-- > 0;)
116 * Divide the number until it fits the given column.
117 * If there will be an overflow by the rounding below,
120 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
123 if (scale & HN_GETSCALE)
126 for (i = 0; i < scale && i < maxscale; i++)
129 /* If a value <= 9.9 after rounding and ... */
130 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
131 /* baselen + \0 + .N */
132 if (len < baselen + 1 + 2)
134 b = ((int)bytes + 5) / 10;
137 r = snprintf(buf, len, "%d%s%d%s%s%s",
138 sign * s1, localeconv()->decimal_point, s2,
139 sep, SCALE2PREFIX(i), suffix);
141 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
142 sign * ((bytes + 50) / 100),
143 sep, SCALE2PREFIX(i), suffix);