• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Amount in Words

 
safraz hanas
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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

Justin Fox
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic