![]() |
|
#1
|
|||
|
|||
Converting numbers to wordsI have an assignment that asks for the following:
Write a C++ program that inputs a dollar amount to be printed on a check and: 1) Calls a function to print the amount in check-protected format with leading asterisks if necessary. [For example amount 1230.60 should be written as **1,230.63] Assume that eleven spaces are available for printing an amount. 2) Calls a function to write the word equvalent of the amount. For example, amount 1230.60 should be written as: ONE THOUSAND TWO HUNDRED THIRTY and 60/100 I have no idea how to approach this problem... Can anyone help me with this? Below is my C++ code. (Sorry for the short code, but as you can see, I'm kinda stuck...) CPP / C++ / C Code:
|
|||
|
#2
|
||||
|
||||
Re: Converting numbers to wordsHi thu,
Another good assignment on the cards. First, declare the number as a float, and not int. Because this says so: Quote:
You can do this program by this algorithm:: 1. Find the number of digits in the number n. Let this be length. (you can use % operator only to integer. So, make int number equal to float input...) 2. Use a string say s to store the number in check-protected format. 3. Add the digits of the number one by one in the string. Use a count variable to check whether three digits have been added to the string. 4. If three digits have been added to the string, then add a comma: ',' 5. Go on till the number becomes zero. 6. Finally reverse the string. So, here is an example: Code:
For printing the remaining spaces by '*', you can use the cout.fill method. So, for an example, consider that the string s is equal to 18,624. When we do like this: CPP / C++ / C Code:
The output will be like: Code:
Now, on to the second part of the program: Quote:
For example, like this: CPP / C++ / C Code:
and text[10] = "Ten" and text[11] = "Eleven".. and text[21] = "Thirty" and so on... (Remember the place where the text is stored...) Then, we can check whether each number in the string. If we get a comma, we should check the length of the string. If the length is less than 6, there is only one comma. So, we can print THOUSAND. If the length if less than 9, there are two commas. So, on the encounter of first comma, print MILLION. On the encounter of next comma, print THOUSAND, etc. Here is a sample execution: Code:
Another sample: Code:
So, Do the first part correct. Then Go to the Second part. The second part is a little bit tougher. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Converting numbers to wordsOh la la.. Looks like I've got my hands full with eh. That really helps Paramesh. I'll let you know if I come up with any coding.
|
|
#4
|
|||
|
|||
Re: Converting numbers to wordsI know it's a C++ forum, but I have just created something in PHP which you may find useful
PHP Code:
Last edited by LuciWiz : 12-Dec-2005 at 05:50.
Reason: Please insert your Php code between [php] & [/php] tags
|
|
#5
|
|||
|
|||
Re: Converting numbers to wordshi all,
i tried this program and this is particularly difficult when we try to convert the number to words in 'asian' method(!!!!) i.e., 211123- is pronounced as two lakh eleven thousand one hundred 'and' twenty three but not as 'two hundred and eleven thousand one hundred and twenty three. there is a lot of difference you know. the 'and' in my program is very difficult to arrive at, particularly when either the given number is less than 100 or the hundred's place of the number is a zero. i stumbled there and quit. do u have any suggestion? or u want me to be a little clearer. |
|
#6
|
||||
|
||||
Re: Converting numbers to wordsQuote:
123.4 = "one hundred twenty three and four tenths". So my suggestion is forget the "and". __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#7
|
|||
|
|||
Re: Converting numbers to wordsyou are promptly right mr.walt.
but i think thousands of fin.instt.in asia still needs to convert to this type of conversion as yet. thankq. |
|
#8
|
|||
|
|||
Re: Converting numbers to wordsI'm not a native english speaker myself - please excuse me for the improper use of the "and" part in the code
It shouldn't be too hard to modify the PHP code (if we're talking about it) to suit your needs. Just remove the "and" part and it should be fine then. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary number systems: BCD, twos complement, ones complement, etc. | machinated | Miscellaneous Programming Forum | 6 | 08-Feb-2006 10:51 |
| subscript error in coding | warborules | C Programming Language | 6 | 27-Nov-2005 17:16 |
| sorting numbers through array | zidanefreak01 | C++ Forum | 3 | 26-Jun-2005 02:41 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 08:36 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The