This discussion is archived
8 Replies Latest reply: Sep 7, 2008 9:28 AM by 807589 RSS

Handling money with doubles

800610 Newbie
Currently Being Moderated
Hi Guys

I am placing 0.00 values into a double. The problem is the the last zero gets stripped so I'm left with 0.0 - Is there any way to overcome this other than using String ?
  • 1. Re: Handling money with doubles
    807589 Newbie
    Currently Being Moderated
    use int instead
  • 2. Re: Handling money with doubles
    3004 Newbie
    Currently Being Moderated
    Do not handle money with doubles. Ever.

    Using either integers and count in cents or thousands of base currency ; or use a big decimal.
  • 3. Re: Handling money with doubles
    807589 Newbie
    Currently Being Moderated
    You can format floats and doubles with String.format or java.text.DecimalFormat.

    But using floats and doubles to represent money is generally considered a bad idea anyway. They're floating point binary representations and there can be rounding issues which I won't go into right now. Generally there are two better options:

    1) use java.math.BigDecimal
    2) use an integer, and instead of storing dollars (including fractional dollars), just store cents. So instead of trying to store $1.92 as 1.92 in a double, store 192 in an integer.
  • 4. Re: Handling money with doubles
    807589 Newbie
    Currently Being Moderated
    BigDecimal is the best option, provided performance is not an issue. If performance is an issue, use double.

    With double you can store 10 trillion dollars with an accuracy of 1/10,000 of a cent.

    If you use int for dollars, instead of getting a rounding error, you get an overflow error which is much worse.

    long is a better choice than int for most currency calculations.
  • 5. Re: Handling money with doubles
    3004 Newbie
    Currently Being Moderated
    Sorry, I should have said long, not integer. I keep mentally refering to integers as mathematical integers, but with silent overflows in Java, this confusion can be fatal.
  • 6. Re: Handling money with doubles
    800610 Newbie
    Currently Being Moderated
    Thanks for the replies guys. I set my doubles to bigdecimals. I am trying to carry out an addition and not to sure how it works. Below is what I have:

    total = new BigDecimal((recipientPrice + stampPrice + letterPrice) * counter);

    Can someone tell me where Im going wrong ? Note that recipientPrice, stampPrice and letterPrice are BigDecimals.
  • 7. Re: Handling money with doubles
    807589 Newbie
    Currently Being Moderated
    shaolinf wrote:
    Thanks for the replies guys. I set my doubles to bigdecimals. I am trying to carry out an addition and not to sure how it works. Below is what I have:
    total = new BigDecimal((recipientPrice + stampPrice + letterPrice) * counter);
    Can someone tell me where Im going wrong ? Note that recipientPrice, stampPrice and letterPrice are BigDecimals.
    Luke, use the API. Use the API, Luke.

    But seriously, look at the BigDecimal API for the methods you'll need. It's all there!
  • 8. Re: Handling money with doubles
    807589 Newbie
    Currently Being Moderated
    If you don't use the BigDecimal API you need not have bothered to use BigDecimal at all. Not sure why you do in any case as there should be no difference between 0.0 and 0.00 except how it is displayed.