• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Convert numeric to words

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I convert numeric to words. Say if the user supplies 465 convert it to four hundred and fifty six, convert 34 to thirty four and so on.
Please give me some pointers in this regard.

Thanks.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First link from Google:

http://www.rgagnon.com/javadetails/java-0426.html
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Morrow:
First link from Google:

http://www.rgagnon.com/javadetails/java-0426.html



THanks a lot.
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The problem with this code is that it does a lot of if conditions. If I need to increase it to display hunderds I'll need to add another if condition checking if the number is greater than 99 and less than 1000 ....
Can someone please suggest a better approach in simple words?

(Marilyn removed some code)
[ August 03, 2005: Message edited by: Marilyn de Queiroz ]
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry, didn't quite understand it.

What didn't you understand about it?

Can someone please suggest a better approach in simple words?

Yes; use the code from the link!
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your code, Smita, is far better than the code in the link. I think you have a good start. Three if statements is not bad. When you need numbers greater than 999, you should be able to reuse the code you have written up to that point.
[ August 03, 2005: Message edited by: Marilyn de Queiroz ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about how to relate the length of the input string to the word you need. For string length 1-3 you're dealing in hundreds. For string length 4-6 you're dealing in thousands, 7-9 in millions.

I'd try something like

That's pure brainstorming - sure to be wrong enough that you'll have fun fixing it.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I promised that post would be wrong! The first three digits have no word following them. If you can convert 123 into "One hundred twenty three" then converting 123,000 into "One hundred twenty three thousand" should be a piece of cake.

Rather than work things out from the length of the string (number of digits) you could just start with the largest word & power of 10 you plan to support, say "billion" and "1,000,000,000".
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, since this is a Cattle Drive assignment, I think I'll move it there for further discussion.

(I purposely left Stan's code since he admits it doesn't even work)
[ August 03, 2005: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn removed code
[ February 10, 2006: Message edited by: Marilyn de Queiroz ]
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic