aspose file tools*
The moose likes Java in General and the fly likes print 123-as-one hundred twenty three Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "print 123-as-one hundred twenty three" Watch "print 123-as-one hundred twenty three" New topic
Author

print 123-as-one hundred twenty three

Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
Hi.



im tryng to develop a code that can print any digit into words,eg-
/*
100-one hundred

1999-one thousand nine hundred ninty nine

5087-five thousand eighty seven
*/
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

OK, cool. So what have you done so far?

Here's what I would do: first, write a program that works only for the numbers 1 to 10. Test it, make sure it works for all values. Then extend it to work for values up to nineteen, and test it again. Then add support for 20-29, and test again.

Now you do something a little different, I think. Adding support for 30-39 is going to be more a generalization than an addition. When it works, test some more.

Then add support up to 99, and test some more.

Then add support for "100", and test some more.

Now add support up to 199, and test some more...

By breaking this into small, manageable problems, you'll have a much easier time of it.


[Jess in Action][AskingGoodQuestions]
Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
thnk you sir , i will try it.
Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
I don't think I would go about it quite like that.

Think about how YOU know how to say a number...

like 5280

you see:
5 in the 1000's so 5 thousand
2 in the 100's so 2 hundred
8 in the 10's this is trickier since unlike >100 each group of 10 has a name
0 in the 1's which is of course just the number.


So it seems to me you really need to start with three modules:

A module to convert 0-9 which will be used by 1's and anything higher than 10's.

A module to convert 10 - 90. Of course this is only 9 different cases (ten,twenty,thirty,etc)

A module which knows the names of each step up (hundred, thousand,million,etc).



So the over all design in my mind is you would work from left to right on a string number.



Take the leftmost digit (5) pass it to a converter to get 5 = five
Pass its position in the string (5280: 5 is in the 4 spot) to the converter which gives you thousand

Take the next digit (2) pass it to the converter to get 2 = two
Pass its position in the string (5280: 2 is in the 3 spot) to the converter which gives you hundred
Append this result to the above

Take the next digit (8) Since this is in the 2nd position (10's) its a special case. Pass it to the special converter to get eighty
Append this result to the above

Take the next digit (0) pass it to the converter to get 0 = null (return nothing)


And we are done having converted 5280 = five thousand two hundred eighty


So its a pretty straight forward iterative process... If you wanted you could do it recursively rather than iteratively but the mechanics wouldn't change much.

Anyway, thats how I see the problem. Good luck.
Tomendra Singh
Ranch Hand

Joined: Jun 28, 2008
Posts: 47
thank you mr.Seamus Minogue i will try it also.
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Originally posted by Seamus Minogue:
A module to convert 10 - 90. Of course this is only 9 different cases (ten,twenty,thirty,etc)

I count 18 cases: "ten", "eleven", "twelve", ... "nineteen", "twenty", "thirty", etc.
Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
Ya this turned out to be a little more complicated than I had layed out but still an enjoyable exercise.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print 123-as-one hundred twenty three