Converting Digits to Words in LANSA

Date:Archived
Product/Release:LANSA for the AS/400, LANSA for Windows
Abstract:Converting a number to into the corresponding word that represents the number
Submitted By:LANSA Technical Support

A number (numeric field) in LANSA can be converted to the corresponding word that represents the number (e.g change '9' to 'nine').

The following RDML function can be used to do this (Don't forget this RDML code can be pasted from here to a LANSA for the AS/400 or LANSA for Windows system. Check out the tip on how to cut and paste):

DEFINE     FIELD(#WORDSTR) TYPE(*CHAR) LENGTH(50)
DEFINE     FIELD(#D1) TYPE(*DEC) LENGTH(2) DECIMALS(0)
DEFINE     FIELD(#W1) TYPE(*DEC) LENGTH(2) DECIMALS(0)
DEFINE     FIELD(#DIGITS) TYPE(*DEC) LENGTH(30) DECIMALS(9) LABEL('Digits') DESC('Digits') EDIT_CODE(L)
DEFINE     FIELD(#WORDS) TYPE(*CHAR) LENGTH(256) LABEL('Words') DESC('Words') INPUT_ATR(LC)
DEF_ARRAY  NAME(#WD) INDEXES(#W1) OVERLAYING(#WORDSTR) TYPE(*CHAR) TOT_ENTRY(10) ENTRY_LEN(5)
CHANGE     FIELD(#WORDSTR) TO('''One  Two  ThreeFour Five Six SevenEightNine Zero ''')
BEGIN_LOOP
DISPLAY    FIELDS((#DIGITS *IN) (#WORDS))
EXECUTE    SUBROUTINE(PROCESS)
END_LOOP
/*                                            */
SUBROUTINE NAME(PROCESS)
/* Initialise output and first character fields  */
CHANGE     FIELD(#WORDS) TO(*DEFAULT)
/* Loop through the whole input string     */
BEGIN_LOOP USING(#D1) FROM(1) TO(30)
CHANGE     FIELD(#W1) TO(*DEFAULT)
SUBSTRING  FIELD(#DIGITS #D1 1) INTO_FIELD(#W1 2)
IF         COND('((#W1 >= 0) and (#W1 <= 9))') if cond('(#w1="0)')" 
change field(#w1) to(10)
endif
use builtin(bconcat) with_args(#words #wd#w1) to_get(#words) endif end_loop /* */ endroutine 

With a little extra work you can add the words "Dollars and Cents" etc to the words.

This can be "translated" to work for any language.