1 /* $NetBSD: number.c,v 1.12 2008/07/20 01:03:21 lukem Exp $ */
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)number.c 8.3 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: number.c,v 1.12 2008/07/20 01:03:21 lukem Exp $");
46 #include <sys/types.h>
55 #define MAXNUM 65 /* Biggest number we handle. */
57 static const char *const name1
[] = {
58 "", "one", "two", "three",
59 "four", "five", "six", "seven",
60 "eight", "nine", "ten", "eleven",
61 "twelve", "thirteen", "fourteen", "fifteen",
62 "sixteen", "seventeen", "eighteen", "nineteen",
65 "", "ten", "twenty", "thirty",
66 "forty", "fifty", "sixty", "seventy",
70 "hundred", "thousand", "million", "billion",
71 "trillion", "quadrillion", "quintillion", "sextillion",
72 "septillion", "octillion", "nonillion", "decillion",
73 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
74 "quindecillion", "sexdecillion",
75 "septendecillion", "octodecillion",
76 "novemdecillion", "vigintillion",
79 int main(int, char *[]);
80 static void convert(char *);
81 static int number(const char *, int);
82 static void pfract(int);
83 static int unit(int, const char *);
84 static void usage(void) __dead
;
97 while ((ch
= getopt(argc
, argv
, "l")) != -1)
111 fgets(line
, sizeof(line
), stdin
) != NULL
; first
= 0) {
112 if (strchr(line
, '\n') == NULL
)
113 errx(1, "line too long.");
115 (void)printf("...\n");
119 for (first
= 1; *argv
!= NULL
; first
= 0, ++argv
) {
121 (void)printf("...\n");
136 for (p
= line
; *p
!= '\0' && *p
!= '\n'; ++p
) {
144 if (isdigit((unsigned char)*p
))
148 if (fraction
!= NULL
)
158 badnum
: errx(1, "illegal number: %s", line
);
164 if ((len
= strlen(line
)) > MAXNUM
||
165 (fraction
!= NULL
&& (flen
= strlen(fraction
)) > MAXNUM
))
166 errx(1, "number too large, max %d digits.", MAXNUM
);
169 (void)printf("minus%s", lflag
? " " : "\n");
174 rval
= len
> 0 ? unit(len
, line
) : 0;
175 if (fraction
!= NULL
&& flen
!= 0)
176 for (p
= fraction
; *p
!= '\0'; ++p
)
179 (void)printf("%sand%s",
182 if (unit(flen
, fraction
)) {
191 (void)printf("zero%s", lflag
? "" : ".\n");
208 if (number(p
, off
)) {
210 (void)printf(" %s%s",
211 name3
[len
/ 3], lflag
? " " : ".\n");
215 for (; len
> 3; p
+= 3) {
219 (void)printf(" %s%s",
220 name3
[len
/ 3], lflag
? " " : ".\n");
224 if (number(p
, len
)) {
244 (void)printf("%s hundred", name1
[*p
- '0']);
249 val
= (p
[1] - '0') + (p
[0] - '0') * 10;
254 (void)printf("%s", name1
[val
]);
256 (void)printf("%s", name2
[val
/ 10]);
258 (void)printf("-%s", name1
[val
% 10]);
266 (void)printf("%s", name1
[*p
- '0']);
276 static const char *const pref
[] = { "", "ten-", "hundred-" };
280 (void)printf("tenths.\n");
283 (void)printf("hundredths.\n");
286 (void)printf("%s%sths.\n", pref
[len
% 3], name3
[len
/ 3]);
294 (void)fprintf(stderr
, "usage: number [# ...]\n");