2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95";
44 #include <sys/types.h>
53 #define MAXNUM 65 /* Biggest number we handle. */
55 static char *name1[] = {
56 "", "one", "two", "three",
57 "four", "five", "six", "seven",
58 "eight", "nine", "ten", "eleven",
59 "twelve", "thirteen", "fourteen", "fifteen",
60 "sixteen", "seventeen", "eighteen", "nineteen",
63 "", "ten", "twenty", "thirty",
64 "forty", "fifty", "sixty", "seventy",
68 "hundred", "thousand", "million", "billion",
69 "trillion", "quadrillion", "quintillion", "sextillion",
70 "septillion", "octillion", "nonillion", "decillion",
71 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
72 "quindecillion", "sexdecillion",
73 "septendecillion", "octodecillion",
74 "novemdecillion", "vigintillion",
77 void convert __P((char *));
78 int number __P((char *, int));
79 void pfract __P((int));
80 void toobig __P((void));
81 int unit __P((int, char *));
82 void usage __P((void));
95 while ((ch = getopt(argc, argv, "l")) != -1)
109 fgets(line, sizeof(line), stdin) != NULL; first = 0) {
110 if (strchr(line, '\n') == NULL)
111 errx(1, "line too long.");
113 (void)printf("...\n");
117 for (first = 1; *argv != NULL; first = 0, ++argv) {
119 (void)printf("...\n");
129 register flen, len, rval;
130 register char *p, *fraction;
133 for (p = line; *p != '\0' && *p != '\n'; ++p) {
145 if (fraction != NULL)
155 badnum: errx(1, "illegal number: %s", line);
161 if ((len = strlen(line)) > MAXNUM ||
162 fraction != NULL && (flen = strlen(fraction)) > MAXNUM)
163 errx(1, "number too large, max %d digits.", MAXNUM);
166 (void)printf("minus%s", lflag ? " " : "\n");
171 rval = len > 0 ? unit(len, line) : 0;
172 if (fraction != NULL && flen != 0)
173 for (p = fraction; *p != '\0'; ++p)
176 (void)printf("%sand%s",
179 if (unit(flen, fraction)) {
188 (void)printf("zero%s", lflag ? "" : ".\n");
198 register int off, rval;
205 if (number(p, off)) {
207 (void)printf(" %s%s",
208 name3[len / 3], lflag ? " " : ".\n");
212 for (; len > 3; p += 3) {
216 (void)printf(" %s%s",
217 name3[len / 3], lflag ? " " : ".\n");
221 if (number(p, len)) {
234 register int val, rval;
241 (void)printf("%s hundred", name1[*p - '0']);
246 val = (p[1] - '0') + (p[0] - '0') * 10;
251 (void)printf("%s", name1[val]);
253 (void)printf("%s", name2[val / 10]);
255 (void)printf("-%s", name1[val % 10]);
263 (void)printf("%s", name1[*p - '0']);
273 static char *pref[] = { "", "ten-", "hundred-" };
277 (void)printf("tenths.\n");
280 (void)printf("hundredths.\n");
283 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
291 (void)fprintf(stderr, "usage: number [# ...]\n");