I have had a situation where the amount had to be converted into words so that it can be printed on checks. A little googling got me to this solution. I had to convert it to the free form version for my use. There might be other/better versions out there, but this worked for me. You can find the original version (if the link is still active) here
// =============================================================
// = Service program... NbrToWords / CvtNbrToWords
// = Description....... Service program to convert a number
// = to words
// = Amount needs to be converted into words for printing on
// = checks.
// =
// = CrtRPGMod Module( Your library/NbrToWords ) +
// = SrcFile( Your library/YourSrcFile )
// =
// = CrtSrvPgm SrvPgm( Your library/NbrToWords ) +
// = Export( *All ) +
// = ActGrp( *Caller )
// =============================================================
H NoMain
// -------------------------------------------------------------
// - Procedure prototypes -
// -------------------------------------------------------------
D CvtNbrToWords Pr 200A
D Number 15S 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 200A
D Nbr 15S 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 200A Inz
// -------------------------------------------------------------
// - Convert number to words - logic -
// -------------------------------------------------------------
/Free
Select;
When Nbr = *Zero;
RtnWords = 'zero';
Other;
If Nbr < *Zero;
RtnWords = 'negative';
Nbr = Nbr * -1;
EndIf;
EvalR AlphaNbr = %EditC(Nbr:'X');
DoW Count <>
Count += 1;
Pos = (Count * 3) - 2;
Work3 = %Subst(AlphaNbr : Pos : 3);
If Work3 <> '000';
If Work1 <> '0';
Clear Idx;
Idx = %Int(Work1);
RtnWords = %TrimR(RtnWords) + ' ' +
%TrimR(Words(Idx)) + ' hundred';
EndIf;
If Work2 <> '00';
Clear Idx;
Idx = %Int(Work2);
RtnWords = %TrimR(RtnWords) + ' ' + %TrimR(Words(Idx));
EndIf;
RtnWords = %TrimR(RtnWords) + ' ' + %TrimR(Grps(Count));
EndIf;
EndDo;
EndSl;
RtnWords = %Trim(RtnWords);
Return RtnWords;
/End-Free
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
1 comment:
Very cool. Thanks.
On the line that starts the loop, it says:
DoW Count <>
But, what should be on the right of the <> ?
Thanks!
Post a Comment