Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: Adding values that contain a dollar sign.

  1. #1
    Lets Assume is offline Assuming
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Adding values that contain a dollar sign.

    First, keeping the dollar sign is kind of crucial, so getting rid of it is not an option.

    I have 7 JTextFields that hold a monetary value. i.e. $14.99, $139.99, etc. How can I make an 8th JTextField add all of these up, while keeping the dollar sign there? What specifically should I look into so I can achieve this?

    Any help is appreciated.

    Thanks,
    Assume

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,945
    Rep Power
    4

    Default Re: Adding values that contain a dollar sign.

    Well, I am unaware of any method which will ignore currency symbols when doing math. You can always treat the amount as a String and test the first character to see if it is a digit or decimal point. If it is, then just convert to a double (or BigDecimal). If it is a dollar sign then just take the substring starting at index 1 and proceed as before.

    Note: when working with money rounding errors can be a problem. A recommended approach is to mutiply your values by 100 and work in cents. Store these in an int or long as appropriate. Then later, assign to a double and divide by 100 to get the cents back. Then convert to a string and prepend a $.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Adding values that contain a dollar sign.

    You need a class that can parse a number String when it is displayed as an amount of money, and you also need a class that can format a number into a String so that it displays as a monetary amount. Fortunately there is a single class that does both, NumberFormat. I recommend that you get a currency instance of this class via the static method NumberFormat.getCurrencyInstance(), and use the object returned to parse the Strings held by your 7 JTextFields into doubles using its parse method, and then after you've added the doubles convert the number back into a currency String using its format method.

    e.g.,

    Java Code:
    import java.text.NumberFormat;
    import java.text.ParseException;
    
    public class CurrencyInstanceEg {
       public static void main(String[] args) {
          String moneyString1 = "$1,250,000.50";
          String moneyString2 = "$250.50";
    
          NumberFormat currencyInstance = NumberFormat.getCurrencyInstance();
          currencyInstance.setGroupingUsed(true);
    
          try {
             double moneyValue1 = currencyInstance.parse(moneyString1)
                   .doubleValue();
             double moneyValue2 = currencyInstance.parse(moneyString2)
                   .doubleValue();
    
             double moneyValueSum = moneyValue1 + moneyValue2;
    
             System.out.printf("%s + %s = %s%n", moneyString1, moneyString2,
                   currencyInstance.format(moneyValueSum));
          } catch (ParseException e) {
             e.printStackTrace();
          }
       }
    }
    which returns:

    $1,250,000.50 + $250.50 = $1,250,251.00
    Last edited by Fubarable; 03-10-2013 at 05:22 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Adding values that contain a dollar sign.

    Why is the '$' sign important?
    Is the user entering the $?

    This strikes me as a classic case of mistaking display values for data values.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 3
    Last Post: 03-24-2012, 07:44 AM
  2. Dollar sign and DecimalFormat
    By javauserjava in forum New To Java
    Replies: 7
    Last Post: 04-05-2011, 11:57 PM
  3. Dollar sign in a while statement
    By oneprotect in forum Advanced Java
    Replies: 3
    Last Post: 01-18-2011, 11:01 AM
  4. Replies: 7
    Last Post: 04-09-2009, 03:24 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •