Results 1 to 3 of 3
  1. #1
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default Handling Money, int, double,...?

    What would be the best way to handle money and the comparison of money(USD)?

    Right now I'm using double and Decimal format to display $x.xx which is the difference between 2 numbers.

    Java Code:
    n = h - l;
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    		
    return twoDForm.format(n);
    Sometimes the actual value of n has more than 2 decimal places, so the code above is not always accurate.

    I was thinking of converting the dollar value to cents and doing comparisons on that, then converting back to dollars. Would that be a good idea or is there a better way of doing this?

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Handling Money, int, double,...?

    Using a floating point number to represent money can lead to rounding errors. You can store the amount as cents in an int. Or you can create your own Cash class that has two instance variables: dollars and cents. The advantage of writing your own class is that you can add as many helper methods as you need.

  3. #3
    bobocheez is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default Re: Handling Money, int, double,...?

    Creating another class didn't even cross my mind.
    Thanks

Similar Threads

  1. Money Problems ... double dose not work ..
    By mobosecomin in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 12:34 AM
  2. Handling double pointers in Java?
    By mattie in forum New To Java
    Replies: 1
    Last Post: 01-16-2011, 12:06 AM
  3. handling double pointer in Java?
    By mattie in forum Advanced Java
    Replies: 0
    Last Post: 01-15-2011, 11:48 PM
  4. Work & Earn money from home, use rebate lower price save money $$$
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 05:57 AM
  5. Using double for money is a sin
    By fishtoprecords in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 07:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •