July 26th, 2004, 04:23 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
| Converting number amount to string representation
Im trying to figure out a way to change a number amount (in this case lets say its some dollar amount) to a string representation. Now its not just some type conversion here....lemme give you an example of what I mean.
Number_Amount = 123.45 -> String_Amount = "One Hundred Twenty-Three and Forty-five cents"
Its just a little critical thinking project that we need to write psuedo code for. So what do you think is a good approach to this? My first thought when I read the problem was to maybe convert it to a string outright, then count the length of the string. So in this example, the string length would be 6 (0-5). I could then subtract 3 from the total length and it would give me the number of places to the left of the decimal point which in this case is 3. Then I could tell that it was in the hundreds. Then from there I could create a fancy for loop or somethign to find out what each number was and then write out its word equivalent.
Any other thoughts would be appreciated....thought it was a good thinking question.
__________________
YAH! I knew you'd be jealous
|
| |
July 26th, 2004, 04:26 PM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001 Location: Pasadena, CA
Posts: 2,152
|
Now that I actually think about it for 2 more minutes after the post (hehe) I realized that what if there wasn't a decimal point....I'd have to first check to see if there is a decimal point because if there wasn't my whole subtract 3 from the length wouldn't work. UNLESS I forced a .00 at the end. hmmm... |
| |
July 26th, 2004, 04:34 PM
|
#3 (permalink)
| | Ultimate Member
Join Date: Jun 2004 Location: Indianapolis, Indiana
Posts: 1,386
| Quote: |
Originally Posted by Tekk Im trying to figure out a way to change a number amount (in this case lets say its some dollar amount) to a string representation. Now its not just some type conversion here....lemme give you an example of what I mean.
Number_Amount = 123.45 -> String_Amount = "One Hundred Twenty-Three and Forty-five cents"
Its just a little critical thinking project that we need to write psuedo code for. So what do you think is a good approach to this? My first thought when I read the problem was to maybe convert it to a string outright, then count the length of the string. So in this example, the string length would be 6 (0-5). I could then subtract 3 from the total length and it would give me the number of places to the left of the decimal point which in this case is 3. Then I could tell that it was in the hundreds. Then from there I could create a fancy for loop or somethign to find out what each number was and then write out its word equivalent.
Any other thoughts would be appreciated....thought it was a good thinking question. | Sounds like a good idea to me. Use a modulus operator starting at 123 % 100 = 23 to get the remainder and take the integer representation of the division result (123 / 100) to get the current number in the 100's place, do the modulus and then division trick again to get the number in the 10's place, etc. Once you have each individual number separated out, say in a linked list, you can then work on the logic behind represnting numbers with text.
This modulus / division trick ALSO works for the cents, so you don't need to worry about them. For instance, 0.56 % .1 = .06 (remainder). .56 / .10 = 0.5 (use an integer typecast to truncate the .06). Then work on the 0.06. Like I said, once you have the numbers all separated out into individual variables, the rest is just text logic.
I think the simplest way to do that would be to figure out using various samples in your mind the way people say numbers. For instance, anything over the teens (19) follows the standard "twenty-two, twenty-three" etc. format where only the part before the hyphen is new - the part after is just "one, two, three, etc" which you already used in programming them for the actual numbers one, two, three, etc. I hope you follow.
Sorry if this is confusing!
Last edited by FatalException : July 26th, 2004 at 04:37 PM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |