Tuesday, November 4, 2008

Convert amount to words in RPG ILE

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
                                                 

Mom of all Bailouts - smart timing?

One thing about the bailout package for the US financial sector that stumps me is the timing of it all. It is so obvious that the timing would work to the advantage of the Democrats that it begs the question: why did Bush go along with the plan?

Did President Bush have something else at the back of his mind when he initiated this move? By proposing to inject about a trillion greenbacks into the financial world, was he trying to preempt a prospective Obama presidency from having the leverage to be able to spend more in the future? Was he trying to somewhat limit the ability of the future President to spend beyond his means? If that was the objective, how is it all going to play out for a prospective McCain Presidency?

The way I see it, regardless of the promises made on the stump, the big spending initiative right at the cusp of the elections will change the way the next administration will have to govern. President Bush has, with one swipe, put a damper on the Democrat thirst for over-spending by doing exactly what they would want to do – increase spending! Obama has already been talking about a trillion in additional spending during his administration and President Bush’s action will somewhat limit his ability to deliver on that promise. Even if he does do that, he will have to do it by either raising taxes higher or by spreading the tax net wider which will surely make him pretty unpopular, thus clearing the playing field for a much more conservative future administration. As a bonus, an Obama win effectively puts off a possible Clinton Presidency by at least 8 years!

If McCain wins the Presidency, it will be upto him and Palin to come up with innovative ways to fund the bailout package without increasing the tax rates. They will act as an effective counter check to the spend-thirsty Congress. Of course, even they will be limited and cash strapped, but if they are dead serious about eliminating earmarks, they will be able to find the dough to fund their campaign promises.

No matter how this election pans out, President Bush’s last spending initiative is sure to leave the next administration with a bigger challenge then they had initially bargained for. One of my major gripes with President Bush’s policies has been his liberal spending throughout his term, but if he really did initiate the biggest of them all to help limit future spending, touché!