Operating System - HP-UX
1822523 Members
2515 Online
109642 Solutions
New Discussion юеВ

Re: Numbers -> Words for Check Printing

 
SOLVED
Go to solution
Doug Dell
Advisor

Numbers -> Words for Check Printing

Hello Everyone:

Our Finance Department has just purchased a new printer and check forms. Of course, no one in IT was contacted until after the fact. The check format is supposed to be identical to our old form. Of course, now we want some 'minor changes' and we don't have the source code. The vendor wants several thousand dollars for this change and 3 months. I would like to write a filter which grabs the output before sending it to lp. The changes actually aren't all that big except for one that is very difficult for me. Currently, the amount (on line 24) prints like 9145.50 and then on line 30 like '9145 and 50/100'. We would like the amount to print like '$*******9,145.50' (with comma's) and 'Nine thousand one hundred fourty-five and 50/100'. I think I can put the comma's in the amounts but the numbers to words conversion has me baffled. The actual format is very simple, each check is 48 lines long and everything except lines 24 and 30 can pass unchanged. The new printer also needs a formfeed every page to trigger the fold and seal operation but that I can do. I've attached a sample file. Any ideas are welcome and points will be given. I'll take anything, awk, C, sed, shell scripts. Please help!!!

Thanks, Doug
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Hi Doug:

An interesting problem. I love it when things like that on sprung on me. The good news is that I have a C routine that I have used for over twenty years to do the numbers -> english conversion.

You use it like this:

english_numbers(345.12,s_tmp,60)

That will format the string s_tmp up a maximum of 60 characters like "Three hundred fourty five and 12/100". To be safe, I would declare s_tmp[128] or so.


Have fun, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Hi Again Doug:

I don't normally do this but this is an interesting one. I think I can translate that C routine into Perl in under 20 minutes. There is a Perl module that is fairly close but I think I'll translate my C routine. I know the commas and stars will be a piece of cake. I should be able to throw everything together by sometime AM CST. I actually have all the pieces except for the english_numbers I just have to put them together.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Numbers -> Words for Check Printing

Hi again Doug:

The Perl version was a snap. I think you will be amazed at the execution speed especially when compared to awk. I managed to throw in your formfeeds and commas as well. The commify
was lifted from an O'Reilly 'Perl Cookbook' example. It illustrates Perl at its best and worst. This guy reverses the string since processing and looking for the decimal is easier; does some in-place editing; and then reverse the string again for output. Believe it or not, that stuff begin to make sense after a bit. You will probably have to play just a little to get the horizontal positioning of the two modified lines correct. Just change the "%50s" in the format string to "%48s" or whatever. Other than that your there. Rather than the thousand of dollars your vendor wanted, I'll settle for a few hundred.

Last note: This guy is a filter so it expects you to feed it with stdin and it simply writes to stdout.


Regards, Clay
If it ain't broke, I can fix that.
Tom Geudens
Honored Contributor

Re: Numbers -> Words for Check Printing

Hi,
Well ... I was gonna suggest using awk with the sub or gsub function, something like
echo 9865 | awk '{ gsub(9,"nine "); gsub(8,"eight "); print }'

Which would still leave some problems ...
but then I read Clay's answer ... and felt
rather stupid.
Doug, give him points from me as well ... if
you would be so kind ...

Tom Geudens
A life ? Cool ! Where can I download one of those from ?
Suhas_3
Advisor

Re: Numbers -> Words for Check Printing

I had written one awk script for number to work conversion. It can be enhanced changing "printf" statements to print in format that you need.
Hope this helps,
Regards,
Suhas
Doug Dell
Advisor

Re: Numbers -> Words for Check Printing

Wow! You guys are great. Suhas your awk script was good but Clay's perl script ran the first time!!! Tom, you think you feel rather stupid, I had been staring at this for hours and had gotten nowhere!

Clay, I hate to ask but now they are so pleased with 'my' work that they found this small problem that they just know I can fix. "Doug, can you put the hyphen in fourty-five?" Can you at least give me a hint? Your perl script looks easy but everytime I start trying to put in the hyphens I get lost.

Sorry, it took me so long to assign points. I got started and then couldn't get back in to the Forums.

Again, thanks to all!!!
Doug
Patrick Wallek
Honored Contributor

Re: Numbers -> Words for Check Printing

Have you tried putting the hyphens into the section where he declares 'my @tens' so that they read "twenty-", "thirty-", etc.?

I don't know perl, but that would appear to be the place to do it.
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Hi Doug:

I'm glad you liked it. As for those hyphens, I think that is 'too much sugar for a dime'. I will give you a hint. If you look for /ty / that will get you started but the rules get complex rather quickly. Besides us dumb folks in Mississippi do know nothing about no hyphens nohow. I'm not even sure (and really don't care) if it's "One hundred fourty-four thousand fourty-four and 44/100" or "One hundred fourty four thousand fourty-four and 44/100". Maybe some of them educated folks knows the answer but I sure don't. Anyway, if you (or some other nosey-body) can find the answer then MAYBE I'll think about it. I got me a new Perl module and that's all I care about. You can tell them folk's in the Finance Dept. who worries about them there hyphens to get a life.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Sorry Patrick, thank's for playing but
"One hundred fourty-five" gets one but "One hundred fourty" does not. It gets worse if the thousands or millions also are supposed to get them there hyphens (what we call dashes) as well.

Regards, Clay

If it ain't broke, I can fix that.
Doug Dell
Advisor

Re: Numbers -> Words for Check Printing

Clay,

My experts tell me that 'fourty-five' gets a hyphen each time in the amount string. Please help.

Patrick,

Thanks, I tried that first but as Clay pointed out that doesn't quite work.

Doug
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Okay Doug:

I spent about 10 minutes after work adding your dumb ole hyphens. I probably didn't code this like a real Perl programmer but rather like a C/C++ guy would. The hyphen stuff is commented a bit to help. If was really an easy addition and ran the first time. I have NOT tested it very extensively so I would test it heavily but the logic appears solid.

I'm still waiting for my check and this project is no longer interesting but you can now be a hero to your users.

Regards, Clay
If it ain't broke, I can fix that.
Doug Dell
Advisor

Re: Numbers -> Words for Check Printing

Thanks again Clay!!!

I had been working on the fix but as usual I just managed to clobber your code and got lots of syntax errors. I was writing another subroutine to add the hyphens but you just added it to the same one. Is that better?

P.S. I'm just asking so that I can give you some more points. You really earned these and you have really made me look like a hero. Thanks again to you and to HP for this Forum. It really is the best!!!

Doug
A. Clay Stephenson
Acclaimed Contributor

Re: Numbers -> Words for Check Printing

Hi Doug:

I added the hyphen stuff to the same routine because it uses the @tens and @units arrays. If I had created another subroutine, I would have had to move the arrays out to file scope. I like to keep as much within a subroutine as possible to avoid side effects and unintentional use of variables. It my coding style and I'm sticking to it.

Regards, Clay
If it ain't broke, I can fix that.