This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interest" Watch "Interest" New topic
Author

Interest

Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
I need some help here...

Firstly, I am using a formulae to get the amount balance of money at the end of some year. The answer that i am getting is like this> $45780.19845673344 and on and on....how do i get it like $45780.00 only?

And is that the reason why i am getting the incorrect value of my answer?

Hope any of you here can clarify for me.
Thank you.

M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
well, you're already importing the java.text.DecimalFormat class (although the import statement you're using for it is a little odd-looking; if it were me, i'd just "import java.text.*". that might be just preference, though), and i'd say that's a good start. have you tried looking up DecimalFormal in the API documentation to see if there might be any useful methods in it?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
There are two things to consider. First, using doubles means you're using approximations of decimal values; your calculations will be inexact. You can use BigDecimal to get exact decimal values or limit your calculations to a specified number of decimal places (typically two for money).

If you use a BigDecimal, it will output only the precision you specify, so you wouldn't need to use DecimalFormat/NumberFormat. It looks like you're not anyway, though you import any of its inner class (probably a typo, as M Beck pointed out).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interest
 
Similar Threads
Help on testprogram and subclass please
Having trouble understanding an error code i keep getting. Please help!
Did I do this right? (Interest calculator)
Replicating a simple bank account
Help code problem please