• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interest

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic