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