Home | Our AS/400 Store | View Our Resumes | Tips | Links | Contact Us |
From time to time, I see requests for a routine to convert numbers to words, such as one that might be used in a program to print checks. Service program NbrToWords consists of a single procedure, CvtNbrToWords (Convert number to words), that does just that.
With service program NbrToWords, your applications needn't worry about the details of the conversion. Simply bind the service program to your application program, invoke procedure CvtNbrToWord passing a single numeric parameter, and receive the text in the return value. Note that the text is returned in lowercase, so after receiving the text, you must perform capitalization as necessary.
Procedure CvtNbrToWords supports numbers up to 15 digits (trillions) in length. The procedure accepts whole numbers only. For numbers containing decimal points (such as the values you print on checks), you invoke the procedure once for the whole number (e.g., dollars) and then again for the decimal portion (e.g., cents) of the number.
Service program NbrToWords follows:
* ============================================================= * = Service program... NbrToWords = * = Description....... Service program to convert a number = * = to words = * = = * = CrtRPGMod Module( YourLib/NbrToWords ) + = * = SrcFile( YourLib/YourSrcFile ) = * = = * = CrtSrvPgm SrvPgm( YourLib/NbrToWords ) + = * = Export( *All ) + = * = ActGrp( *Caller ) = * ============================================================= H NoMain * ------------------------------------------------------------- * - Procedure prototypes - * ------------------------------------------------------------- D CvtNbrToWords PR 200 D 15 0 Value * ------------------------------------------------------------- * - Global variables - * ------------------------------------------------------------- D MaxGrps C 5 D Words S 13 Dim( 99 ) D CtData D Grps S 8 Dim( MaxGrps ) D CtData * ============================================================= * = Procedure: CvtNbrToWords = * = Description: Convert number to words = * ============================================================= P CvtNbrToWords B Export * ------------------------------------------------------------- * - Procedure interface - * ------------------------------------------------------------- D CvtNbrToWords PI 200 D Nbr 15 0 Value * ------------------------------------------------------------- * - Variable declarations - * ------------------------------------------------------------- D AlphaNbr S 15 D WorkFld DS D Work3 3 D Work2 2 Overlay( Work3 : 2 ) D Work1 1 Overlay( Work3 : 1 ) D Count S 5I 0 D Pos S 5I 0 D Idx S 5I 0 D RtnWords S 200 Inz( *Blank ) * ------------------------------------------------------------- * - Convert number to words - * ------------------------------------------------------------- C Select C When Nbr = *Zero C Eval RtnWords = 'zero' C Other C If Nbr < *Zero C Eval RtnWords = 'negative' C Eval Nbr = Nbr * -1 C EndIf C Move Nbr AlphaNbr C Do MaxGrps Count C Eval Pos = ( Count * 3 ) - 2 C Eval Work3 = %Subst( AlphaNbr : C Pos : C 3 ) C If Work3 <> '000' C If Work1 <> '0' C Clear Idx C Move Work1 Idx C Eval RtnWords = %TrimR( RtnWords ) + C ' ' + C %TrimR( Words( Idx ) ) + C ' hundred' C EndIf C If Work2 <> '00' C Clear Idx C Move Work2 Idx C Eval RtnWords = %TrimR( RtnWords ) + C ' ' + C %TrimR( Words( Idx ) ) C EndIf C Eval RtnWords = %TrimR( RtnWords ) + C ' ' + C %TrimR( Grps( Count ) ) C EndIf C EndDo C EndSl C Eval RtnWords = %Trim( RtnWords ) C Return RtnWords P CvtNbrToWords E ** CtData Words one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-three forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine seventy seventy-one seventy-two seventy-three seventy-four seventy-five seventy-six seventy-seven seventy-eight seventy-nine eighty eighty-one eighty-two eighty-three eighty-four eighty-five eighty-six eighty-seven eighty-eight eighty-nine ninety ninety-one ninety-two ninety-three ninety-four ninety-five ninety-six ninety-seven ninety-eight ninety-nine ** CtData Grps trillion billion million thousand Here's a sample program, Example, that uses procedure CvtNbrToWords from service program NbrToWords: * ============================================================= * = Program....... Example = * = Description... Convert numbers to words example = * = = * = CrtRPGMod Module( YourLib/Example ) + = * = SrcFile( YourLib/YourSrcFile ) = * = = * = CrtPgm Pgm( YourLib/Example ) + = * = BndSrvPgm( YourLib/NbrToWords ) = * ============================================================= * ------------------------------------------------------------- * - Procedure prototypes - * ------------------------------------------------------------- D CvtNbrToWords PR 200 D 15 0 Value * ------------------------------------------------------------- * - Variable declarations - * ------------------------------------------------------------- D MyNbr S 10I 0 D MyWords S 200 * ------------------------------------------------------------- * - Sample number-word-conversion - * ------------------------------------------------------------- C Eval MyNbr = 1234567898 C Eval MyWords = CvtNbrToWords( MyNbr ) C Eval *InLR = *On
Home | Our AS/400 Store | View Our Resumes | Tips | Links | Contact Us |
Tips and Techniques accumulated by Thomas Bishop from various sources including, but not limited to, Midrange-L, RPG-L, Midrange Computing, and News/400.
|