aspose file tools*
The moose likes Beginning Java and the fly likes Amount in Words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Amount in Words" Watch "Amount in Words" New topic
Author

Amount in Words

safraz hanas
Greenhorn

Joined: Feb 21, 2007
Posts: 10
Hi,

I'm developing a simple Point of Sale application. In the invoice I need to show the amount in words. I don't have any clues how to do it.
Is there any ready-made application, which i can use! or else any tips?

Thanks in Advanced
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
If possible, spell out exactly what it is you are looking for and be as specific as possible. What do you mean by "amount in words"? Do you want a method that takes a double as a parameter and returns a String of the number in english words? Something like this?

Or am I way off base? If this is what you are looking for, ask any of the students in the cattle drive who have completed assignment Java-4b (Say). They will have a ready-made routine that they'd love to share w/ you.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If they "get" the ranch, they won't share. They might give some good hints, though.

Doubles are dangerous for money amounts because they don't hold exactly the values you expect. They will give you a nasty surprise sooner or later. It's safer to use a integer type (int or long) and store the amount * 100. If you're stuck with double - working with code an instructor gave you for example - we can still try to get by.

Let's see how much you are comforatable with so far. If you have a number like 12345 (for $123.45) ... how do you get the cents? The dollars?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
if you were gonna program such a complex subroutine, you would have a lot of if statements wouldn't you?

like first you would have to scan the number to see if it has a decimal.

then count from the decimal (1's place) backwards to know what to print.

if(backtrack == 3)
{
if(currnumber == 1)
{
theOutput += "One Hundred ";
}
}

something like that huh?


Justin Fox


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802



this is the (or one) approach right?
is is there a more effiecient way.

Justin Fox
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you want the String approach (which I think I would not, but not sure) look at NumberFormat. You can always have exactly two decimal places, and avoid something like 3.3333333333333 from a Double.

I'd get money into integer types. To see why, print 1234.56 - 1234.00. Look at Math.round() when doing the conversion. Then you can get the dollar and cents parts with a single operation each.

Try to get results you like from this. It can be exactly this many lines, just with your own code for the ...

Given 1234.56 you'd want to see 1234.56 123456 1234 56. What does it give for 1.00/3.0? Or 2.00/3.0?
 
jQuery in Action, 2nd edition
 
subject: Amount in Words