Results 1 to 2 of 2
  1. #1
    Rageagainst20 is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default Capping decimal places

    Java Code:
    int previous = Integer.parseInt(TxtPrevious.getText());
    int current = Integer.parseInt(TxtCurrent.getText());
    double cpu = 0.07;
    double quart = 20.00;
    
        
        if (current >= previous) 
        {          
            double sum = (current - previous) * cpu + quart;
              String sAnswer = Double.toString(sum);
                       TxtResult.setText ("" + sAnswer);
        } 
            else {
                    JOptionPane.showMessageDialog(null, "Current reading has to be greater than previous reading", "Billing Error", JOptionPane.ERROR_MESSAGE);
        
            }
    The above code takes two values add's 0.07 and 20.00 to it and displays it in a textbox, I was wondering if there was anyway to "cap" the decimals to 2 deimcal places, aka currency format.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  3. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  4. Replies: 7
    Last Post: 01-12-2008, 09:02 PM
  5. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:57 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
  •