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é!

Thursday, October 16, 2008

Why Chiru should tackle corruption

Can we achieve all round prosperity?

Difficult but do-able. Andhra Pradesh is already on the path to higher prosperity, thanks in large part to the previous two administrations. True, there have been obstacles and distractions on the way that we could have done without. And there is rampant corruption that has become deep rooted in the administration that is preventing more rapid development. There have been difficulties that regular people have had to face all along this path and much of this has been the result of rampant corruption.

This is where Chiranjeevi should focus his efforts. This role will not only be a continuation of what he has depicted in a multitude of films (thus making it easier for him to bring this topic to the fore), but dealing with it will also help our state come to the front of the ladder when it comes to overall development. Corruption has been the root cause of many of the problems common people have been facing since independence. To effectively take care of this animal, Chiru has to live for the fight day in and day out. Sure, there will be people within his party and amongst his close family and friends who might already be salivating at the prospects of having a go at a piece of the corruption pie and that is what Chiru will need to address up front. Like the saying goes, cleanup begins at home. Half the battle would be won if he makes it clear that he will not tolerate any corruption from any of his party members. With that out of the way, he will be able to campaign on the anti-corruption slogan.

Once Chiru gets settled down in his new role (that is our hope and objective right?), he should concentrate on efforts to weed out corruption in any shape or form. He might convey his seriousness by forming an anti-corruption ministry and giving it all the powers that it needs to help wipe out corruption from the state, city and local government wings. Incidents of corruptions should be treated as offences against the state and officers indulging in it should be punished. This maybe done by using a graded approach structured on the lines of the point system which is used by police across the world to write up offensive motorists. Offenders can be given points and the number of points earned would determine the severity of punishment. Punishment can range from warnings to demotions to termination with a bad rec. Such a standard should be worked out during the campaigning itself thus letting everybody know that Chiru means business and that he is willing to walk the talk.

 Most Indians are aware that enforcement is a problem anywhere in India and Chiru should signal that he is not going to allow that to happen under his watch. Setting up an anti-corruption ministry whose sole purpose will be to collect information/complaints from the citizens and to act on them by investigating the charges and enforcing the corruption-point system if needed will send a signal to the repeated offenders that they will not be able to get away from the law this time. Consistent enforcement, will over time, make sure that corruption will be reined in thus opening the doors for a more prosperous and happy Andhra Pradesh. Hopefully, this will set an example for the rest of the nation to follow. And Chiru will, forever, become another visionary leader that our state can feel proud of!

Is Obama the medicine America needs?

With but a few weeks to go for the final vote to be cast and the verdict to be sealed and most polls suggesting that Americans are leaning towards an Obama presidency, one cannot but stop and wonder why, for the first time, the US is on the cusp of choosing the most socialist and communist administration in its history.

From another perspective, the recent economic crisis more than anything else, seems to have dwarfed this fact and turned the tide in Obama’s favor; for now. Once the dust settles down and Americans take the time to ponder over their choice, they will realize that by choosing Obama, they have indeed chosen more of the same vis-à-vis Jimmy Carter. And as time goes by, they will realize that more of the same a la George Bush (though, in my opinion, McCain is more liberal than George Bush) would have been way better for their day to day lives than more of the same a la Jimmy Carter. Of course, by then, it will be too late for them to reverse their choice, at least for another four years. Atleast they would have figured out that Obama might as well been referring to himself when he was suggesting 'more of the same' while on the stump.

The US would be in for much worse conditions with the Obama socialists taking control of the markets and maybe even the daily lives of regular citizens pretty much like what Chavez has been doing in Venezuela over the past few years. This might be the exact medicine that is needed by Americans to jolt them out of the make-believe world that they have been relentlessly prodded into both by a swooning media as well as the fluffy words of Obama that he will deliver them from all the difficulties that they have been facing with a worsening economy. So taken in are they that they don’t have the inclination to go over Obama’s ‘non-achievements’ over the past two decades. We need not dig deep to realize that Obama has mostly been more about sound words and speeches rather than sound judgment. It is no secret that he is the most liberal Senator in the current Congress. There is the prospect that Obama’s USA can, for the first time in history, be positioned as being more socialist than Putin’s Russia or Wen’s China. It looks like none of these arguments will matter for now as most of his supporters tend to paint such arguments as a racial slur rather than take them at face value.

Obama’s tax and wealth redistribution policies might work well in countries where people have been subjugated for generations together, but in a country like the USA where entrepreneurship is rampant, they are deemed to fail. But before they fail, it is going to look like they are succeeding which will prompt him to up the ante. When he does that, the economy is going to take a turn for the worse and Americans will begin to realize the foolhardiness of their decision of electing Obama - albeit a tad late. The souring economy will act as a medicine to rejuvenate regular Americans to wake up and take action at the next elections, but not before putting up with a lot of pain and hardship; a bitter dose of medicine that an Obama administration will force upon them.

It will be much more difficult to turn the country around after four years of a Jimmy Carter like economy, but if Americans could stomach the medicine doled out to them three decades ago and still bring into office a man who single handedly turned around the economy, they can sure do it again. But they will need to elect (pun intended) to take their medicine first.