Prev: New to COBOL
Next: RosettaCode
From: sharad kumar.j on 8 Aug 2010 05:20 hi, I hhave started my programming in cobol and i'm 3 weeks old.I would like to know that is there any built in function to convert amount in number to words eg:345 as three hundred and forty five....
From: Alistair on 8 Aug 2010 08:11 On Aug 8, 10:20 am, "sharad kumar.j" <aryansmit3...(a)gmail.com> wrote: > hi, > I hhave started my programming in cobol and i'm 3 weeks old.I would > like to know that is there any built in function to convert amount in > number to words > eg:345 > as > three hundred and forty five.... Probably not but a quick browse of this news group should offer up a downloadable program to do just that. Otherwise, look up google for cobol and check out the web sites which offer code as someone has already done exactly what you want to do. Otherwise, I don't force pre-school children to do it but, do your own homework.
From: Pete Dashwood on 8 Aug 2010 09:04 sharad kumar.j wrote: > hi, > I hhave started my programming in cobol and i'm 3 weeks old.I would > like to know that is there any built in function to convert amount in > number to words > eg:345 > as > three hundred and forty five.... This post brought back some memories for me. As a young COBOL programmer myself once, I remember being asked to provide a subroutine that would do exactly what you ask. It was to be used for printing the amount in words on cheques. It seemed pretty straightforward: Use a table lookup to convert the words into numbers and the mighty COBOL STRING verb to put the words together. How hard could it be? Doc Dwarf has frequently reminded us here of the dangers inherent in the phrase: "All y' gotta do..." He is absolutely right. I found to my consternation that the words AND, DOLLARS, CENTS and ONLY also needed to be added to the string, and they also wanted the word 'EXACTLY' if there were zero cents in the amount. Some examples: one thousand three hundred and twenty seven dollars and nine cents only. (1327.09) one thousand three hundred and twenty seven dollars exactly (1327.00) one hundred and one thousand three hundred and twenty seven dollars and nine cents only. (101327.09) The placement of DOLLARS, CENTS, ONLY, and EXACTLY is fairly simple, but the placement of AND became problematic and the rules for when it was needed were not entirely clear. Attempting to retrofit to the code I had hastily written resulted in some pretty awful spaghetti... Finally, I decided to sit down and work out all the cases where AND must be added to the string. 1. preceding the cents UNLESS there were no digits to the left of the point, OR the cents were zero. 1. Between hundreds and tens. (three hundred and twenty five). 2. Between thousands and tens IF there are NO hundreds. (one thousand AND twenty seven...) 3. Between hundreds of thousands and tens of thousands. (three hundred AND twenty five thousand, four hundred AND tweny-nine...) Writing this as a bunch of COBOL IFs was not pretty so I applied Boolean simplification and came up with a single compound condition which did it. (In effect it decided if the next word to be added to the string should be a number or the word AND...) I was so proud of this I showed it to the head programmer who checked it in his head and pronounced it OK. (he was a disabled guy with a mind like a razor and I learned much from him. He was the guy who first interested me in propositional calculus and Boolean algebra.) I then rewrote the code from scratch as a callable subroutine, which received a number and returned a string, and remembered the experience right to this day, some 42 years later... :-) If I had the code, I would gladly give it to you, but I think you will have the same fun and sense of achievement I got, if you do it yourself. Havea shot at it and, if you get stuck, post your attempt here. Pete. -- "I used to write COBOL...now I can do anything."
From: Pete Dashwood on 8 Aug 2010 09:09 Alistair wrote: > On Aug 8, 10:20 am, "sharad kumar.j" <aryansmit3...(a)gmail.com> wrote: >> hi, >> I hhave started my programming in cobol and i'm 3 weeks old.I would >> like to know that is there any built in function to convert amount in >> number to words >> eg:345 >> as >> three hundred and forty five.... > > Probably not but a quick browse of this news group should offer up a > downloadable program to do just that. Otherwise, look up google for > cobol and check out the web sites which offer code as someone has > already done exactly what you want to do. > > > Otherwise, I don't force pre-school children to do it but, do your own > homework. :-) Yes, writing COBOL at three weeks of age is quite an achievement. I honestly don't think this is homework but rather a legitimate attempt to avoid re-inventing the wheel. Given English is probably not his first language, he communicated the requirement well and at least he knew where to post. :-) Pete. -- "I used to write COBOL...now I can do anything."
From: Arnold Trembley on 8 Aug 2010 11:47
On 8/8/2010 4:20 AM, sharad kumar.j wrote: > hi, > I hhave started my programming in cobol and i'm 3 weeks old.I would > like to know that is there any built in function to convert amount in > number to words > eg:345 > as > three hundred and forty five.... COBOL Users Groups http://www.cobug.com/ COBOL source code samples http://www.cobug.com/cobug/docs/codesamples0020.html#CONTR This site has a lot of information and code samples for writing out an amount in English (usually done for printed checks) http://www.simotime.com/cbltxn01.htm Kind regards, -- http://www.arnoldtrembley.com/ |