home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1778
Discussions: 188,401, Posts: 2,243,601, Members: 232,629
Old July 26th, 2004, 04:23 PM   Digg it!   #1 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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

Tekk is offline   Reply With Quote
Old July 26th, 2004, 04:26 PM     #2 (permalink)
Ultimate Member
 
Tekk's Avatar
 
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...

Tekk is offline   Reply With Quote
Old July 26th, 2004, 04:34 PM     #3 (permalink)
Ultimate Member
 
FatalException's Avatar
 
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.
FatalException is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crystal Reports 8, number to string table link? Tec Webmastering and Programming 2 July 5th, 2003 12:14 AM
Working With Binary Data In VB 6.0 SpaceMonkey Webmastering and Programming 6 October 3rd, 2002 12:20 AM
another java question jgargac Webmastering and Programming 4 March 8th, 2002 06:58 PM

Most Active Discussions
Is It Just Me? (2906)
Unarmed man on his stomach shot by .. (6)
Misery Loves Company... (2144)
New Build ( Finally ) (6)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
Folderchat Weekday thread (444)
Upgrading RAM (6)
Recent Discussions
How do you move a hard-drive to.. (4)
Laptop proccesor to desktop mob.. (1)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Left 4 Dead Small Freezes (3)
RCA 52Inch HDTV wont turn on (4)
wishin i could edit my aol prof.. (0)
Sporadic internet connectivity (2)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 03:43 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28