1 /* $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
7 * Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org>
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
48 static const int maxscale = 6;
51 humanize_number(char *buf, size_t len, int64_t quotient,
52 const char *suffix, int scale, int flags)
54 const char *prefixes, *sep;
55 int i, r, remainder, s1, s2, sign;
60 /* Since so many callers don't check -1, NUL terminate the buffer */
65 if (buf == NULL || suffix == NULL)
69 else if (scale > maxscale &&
70 ((scale & ~(HN_AUTOSCALE|HN_GETSCALE)) != 0))
72 if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))
75 /* setup parameters */
78 if (flags & HN_IEC_PREFIXES) {
81 * Use the prefixes for power of two recommended by
82 * the International Electrotechnical Commission
83 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
85 * HN_IEC_PREFIXES implies a divisor of 1024 here
86 * (use of HN_DIVISOR_1000 would have triggered
87 * an assertion earlier).
90 divisordeccut = 973; /* ceil(.95 * 1024) */
92 prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
94 prefixes = "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
97 if (flags & HN_DIVISOR_1000) {
101 prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
103 prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
106 divisordeccut = 973; /* ceil(.95 * 1024) */
108 prefixes = "B\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
110 prefixes = "\0\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
114 #define SCALE2PREFIX(scale) (&prefixes[(scale) * 3])
118 quotient = -quotient;
119 baselen += 2; /* sign, digit */
122 baselen += 1; /* digit */
124 if (flags & HN_NOSPACE)
130 baselen += strlen(suffix);
132 /* Check if enough room for `x y' + suffix + `\0' */
133 if (len < baselen + 1)
136 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
137 /* See if there is additional columns can be used. */
138 for (max = 1, i = len - baselen; i-- > 0;)
142 * Divide the number until it fits the given column.
143 * If there will be an overflow by the rounding below,
147 (quotient >= max || (quotient == max - 1 &&
148 (remainder >= divisordeccut || remainder >=
149 divisor / 2))) && i < maxscale; i++) {
150 remainder = quotient % divisor;
154 if (scale & HN_GETSCALE)
157 for (i = 0; i < scale && i < maxscale; i++) {
158 remainder = quotient % divisor;
163 /* If a value <= 9.9 after rounding and ... */
165 * XXX - should we make sure there is enough space for the decimal
166 * place and if not, don't do HN_DECIMAL?
168 if (((quotient == 9 && remainder < divisordeccut) || quotient < 9) &&
169 i > 0 && flags & HN_DECIMAL) {
170 s1 = (int)quotient + ((remainder * 10 + divisor / 2) /
172 s2 = ((remainder * 10 + divisor / 2) / divisor) % 10;
173 r = snprintf(buf, len, "%d%s%d%s%s%s",
174 sign * s1, localeconv()->decimal_point, s2,
175 sep, SCALE2PREFIX(i), suffix);
177 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
178 sign * (quotient + (remainder + divisor / 2) / divisor),
179 sep, SCALE2PREFIX(i), suffix);