Multi-Value Solutions - Jun '97

Numbers to Words

Nathan Rector

Natec Systems

nater@northcoast.com

http://www.northcoast.com/~nater/

Everyone at one time or another has a need to convert numbers to a word style description. This a common problem when creating programs for writing checks. Many companies also like to have their unalterable numbers on contracts to be printed in both numeric form and in an alpha description form.

Since most Multi-value systems don't supply any conversion code or routine for numbers to alpha description, you have to write your own. The program shown here is simple and is fairly quick in its conversion.

I have found this program useful for several things. One is in indexing routines. There are times when a Soundex index is needed, ex. indexing addresses or company names that have numbers. When a Soundex conversion is done, all numbers are excluded. This makes the indexing inaccurate.

By adding the additional option to convert the numbers into a count format instead of a dollar format, I was able to correct this problem.

This routine converts numbers as large as a million, and with very little modification, numbers even larger. The program converts the whole number and decimal as independent values and passes them back in separate variables. This allows the calling program tospecify the delimiter between the whole number description and the decimal description.

SUBROUTINE NUMBER.TO.ALPHA1(NUMBER,INT,DEC,TYPE)

EQUATE AM TO CHAR(254), VM TO CHAR(253), SVM TO CHAR(252)

EQUATE BELL TO CHAR(7)

*

*

*CREATED BY NATHAN RECTOR, 10/09/96

* Natec Systems

* nater@northcoast.com

* http://www.northcoast.com/~nater/

*

*USED IN other programs

*

*

* D O C U M E N T A T I O N

*

* this program is used to convert a number to a word

*

* TYPE = '' or 0 - Dollar format

* 1 - count format: first, second, third

*

* INPUT 'Y' to continue OR 'N' to return to Menu.

*

*********************************************************************

*DECLARING VALUES

*********************************************************************

DEC = "" ; INT = ""

NMBERS = "1" :VM: "2" :VM: "3" :VM: "4" :VM: "5" :VM: "6" :VM: "7" :VM: "8" :VM: "9" :VM: "10"

NMBERS = NMBERS :VM: "11" :VM: "12" :VM: "13" :VM: "14" :VM: "15" :VM: "16" :VM: "17" :VM: "18" :VM: "19"

NMBERS = NMBERS :VM: "20" :VM: "30" :VM: "40" :VM: "50" :VM: "60" :VM: "70" :VM: "80" :VM: "90"

*

ALPHA = "ONE" :VM: "TWO" :VM: "THREE" :VM: "FOUR" :VM: "FIVE" :VM: "SIX" :VM: "SEVEN" :VM: "EIGHT" :VM: "NINE"

ALPHA = ALPHA :VM: "TEN" :VM: "ELEVEN" :VM: "TWELVE" :VM: "THIRTEEN" :VM: "FOURTEEN" :VM: "FIFTEEN" :VM: "SIXTEEN"

ALPHA = ALPHA :VM: "SEVENTEEN" :VM: "EIGHTEEN" :VM: "NINETEEN" :VM: "TWENTY" :VM: "THIRTY" :VM: "FORTY" :VM: "FIFTY" :VM: "SIXTY" :VM: "SEVENTY" :VM: "EIGHTY" :VM: "NINETY"

*********************************************************************

*PROGRAMMING LOGIC

*********************************************************************

*** convert into dollars and cents format

*** divid into two parts.

*

DOLLARS = FIELD(NUMBER,".",1)

CENT = FIELD(NUMBER,".",2)

*

IF DOLLARS = 0 THEN

INT = ""

END ELSE

NMBER = DOLLARS ; GOSUB 600 ; INT = DISPLAY

END

*

IF CENT = 0 THEN

INT = ""

END ELSE

NMBER = CENT ; GOSUB 600 ; CENT = DISPLAY

END

900*

RETURN

*********************************************************************

*PROGRAMMING LOGIC

*********************************************************************

100*

* converts 'units' portion of number

LOCATE(NMBER,NMBERS,1;VAR) ELSE VAR = ""

IF NOT(VAR = "") THEN DISPLAY = DISPLAY : ALPHA<1,VAR> :" "

RETURN

200*

* Convert "ten's" portion of number

NMBERWRK = 0 + NMBER

BEGIN CASE

CASE NMBERWRK > 19

NMBERWRK = NMBER[1,1] :"0"

LOCATE(NMBERWRK,NMBERS,1;VAR) ELSE VAR = ""

IF NOT(VAR = "") THEN

IF TYPE = 1 AND NOT(DISPLAY = "") THEN DISPLAY = DISPLAY :" AND "

DISPLAY = DISPLAY : ALPHA<1,VAR> :"-"

NMBER = NMBER[2,1] ; GOSUB 100

END

CASE 1

LOCATE(NMBERWRK,NMBERS,1;VAR) ELSE VAR = ""

IF NOT(VAR = "") THEN

IF TYPE = 1 AND NOT(DISPLAY = "") THEN DISPLAY = DISPLAY :" AND "

DISPLAY = DISPLAY : ALPHA<1,VAR> :" "

END

END CASE

RETURN

300*

* convert "HUNDERD'S" portion of number

NMBERWRK = NMBER[1,1]

LOCATE(NMBERWRK,NMBERS,1;VAR) ELSE VAR = ""

IF NOT(VAR = "") THEN DISPLAY = DISPLAY : ALPHA<1,VAR> :" HUNDRED "

*

NMBER = NMBER[2,2]

GOSUB 200

RETURN

500*

NMBERFIELD = 1000000000 + NMBER

NMBERFIELD = NMBERFIELD[2,9]

GOSUB 1000

RETURN

600*

DISPLAY = ""

GOSUB 500

*

DISPLAY = TRIM(DISPLAY)

LAST.WORD = FIELD(DISPLAY," ",DCOUNT(DISPLAY," "))

DISPLAY = DISPLAY[1,COL1()]

*

DELIM = " "

IF NOT(FIELD(LAST.WORD,"-",2) = "") THEN

DISPLAY = DISPLAY :" ": FIELD(LAST.WORD,"-",1)

LAST.WORD = FIELD(LAST.WORD,"-",2)

DELIM = "-"

END

*

BEGIN CASE

CASE NOT(TYPE = 1) ;* not count format

CASE LAST.WORD = ""

CASE LAST.WORD = "ONE" ; LAST.WORD = "FIRST"

CASE LAST.WORD = "TWO" ; LAST.WORD = "SECOND"

CASE LAST.WORD = "THREE" ; LAST.WORD = "THIRD"

CASE 1 ; LAST.WORD = LAST.WORD :"TH"

END CASE

*

DISPLAY = TRIM(DISPLAY :DELIM: LAST.WORD)

RETURN

1000*

LN = LEN(NMBER)

BEGIN CASE

CASE LN = 1 ; GOSUB 100

CASE LN = 2 ; GOSUB 200

CASE LN = 3 ; GOSUB 300

CASE LN = 4 OR LN = 5 OR LN = 6

NMBERHOLD = NMBER

NMBER = NMBER[1,LN - 3]

GOSUB 1000

IF NOT(NMBERFIELD[4,3] = "000") THEN DISPLAY = DISPLAY :"THOUSAND "

*

LN = LEN(NMBERHOLD)

NMBER = NMBERHOLD[LN-2,3]

GOSUB 300

CASE LN = 7 OR LN = 8 OR LN = 9

NMBERHOLD = NMBER

NMBER = NMBER[1,LN - 6]

GOSUB 1000

IF NOT(NMBERFIELD[4,6] = "000000") THEN DISPLAY = DISPLAY :"MILLION "

LN = LEN(NMBERHOLD)

NMBER = NMBERHOLD[LN-5,6]

GOSUB 1000

END CASE

RETURN

END