Initial Revision
[ohcount] / test / src_dir / awk1.awk
1 # prchecks - print formatted checks
2 #   input:  number \t amount \t payee
3 #   output: eight lines of text for preprinted check forms
4
5 BEGIN {
6     FS = "\t"
7     dashes = sp45 = sprintf("%45s", " ")
8     gsub(/ /, "-", dashes)        # to protect the payee
9     "date" | getline date         # get today's date
10     split(date, d, " ")
11     date = d[2] " " d[3] ", " d[6]
12     initnum()    # set up tables for number conversion
13 }
14 NF != 3 || $2 >= 1000000 {        # illegal data
15     printf("\nline %d illegal:\n%s\n\nVOID\nVOID\n\n\n", NR, $0)
16     next                          # no check printed
17 }
18 {   printf("\n")                  # nothing on line 1
19     printf("%s%s\n", sp45, $1)    # number, indented 45 spaces
20     printf("%s%s\n", sp45, date)  # date, indented 45 spaces
21     amt = sprintf("%.2f", $2)     # formatted amount
22     printf("Pay to %45.45s   $%s\n", $3 dashes, amt)  # line 4
23     printf("the sum of %s\n", numtowords(amt))        # line 5
24     printf("\n\n\n")              # lines 6, 7 and 8
25 }
26
27 function numtowords(n,   cents, dols) { # n has 2 decimal places
28     cents = substr(n, length(n)-1, 2)
29     dols = substr(n, 1, length(n)-3)
30     if (dols == 0)
31         return "zero dollars and " cents " cents exactly"
32     return intowords(dols) " dollars and " cents " cents exactly"
33 }
34
35 function intowords(n) {
36     n = int(n)
37     if (n >= 1000)
38         return intowords(n/1000) " thousand " intowords(n%1000)
39     if (n >= 100)
40         return intowords(n/100) " hundred " intowords(n%100)
41     if (n >= 20)
42         return tens[int(n/10)] " " intowords(n%10)
43     return nums[n]
44 }
45
46 function initnum() {
47     split("one two three four five six seven eight nine " \
48                         prchecks           "ten eleven twelve thirteen fourteen fifteen " \
49                         prchecks           "sixteen seventeen eighteen nineteen", nums, " ")
50     split("ten twenty thirty forty fifty sixty " \
51          prchecks           "seventy eighty ninety", tens, " ")
52 }
53