Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to round to 2 decimal places in a dialog box

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to round to 2 decimal places in a dialog box

    I know how to print using the printf method, but can't for the life of me get it to round to two decimal places using a dialog box.

    import javax.swing.JOptionPane;
     
    public class Program6 {
     
        public static void main(String[] args) {
            //declare variables
            double loanAmount, annualInterestRate, monthlyPayment, 
            monthlyInterestRate, monthlyInterest, totalPaid;
     
            int month;
     
     
    //promt user to enter amount borrowed
            String loanAmountString = JOptionPane.showInputDialog
                    (null, "Enter the amount to be borrowed in dollars");
            loanAmount = Double.parseDouble(loanAmountString);
     
            //prompt user to enter annual interest
            String annualInterestRateString = JOptionPane.showInputDialog 
                    (null, "Enter the annual interest rate, eg 12 percent");
            annualInterestRate = Double.parseDouble(annualInterestRateString);
            annualInterestRate = (annualInterestRate / 100);
     
           //calculate monthly interest rate
            monthlyInterestRate = annualInterestRate / 12;
     
            //calculate monthly interest payment
            monthlyInterest = loanAmount * monthlyInterestRate;
     
            //display monthly interest
            JOptionPane.showMessageDialog(null, "The Monthly interest is " +
                    monthlyInterest);
     
            //promt user to enter amount to be paid each month
            String monthlyPaymentString = JOptionPane.showInputDialog
                    (null, "Enter the amount to be paid each month. "
                    + "Amount must be greater than monthly interest.");
            monthlyPayment = Double.parseDouble(monthlyPaymentString);
     
     
     
            //parse double
            totalPaid = 0.00f;
            month = 0;
     
            //begin first loop
            while (monthlyPayment <= monthlyInterest) {
          String newMonthlyPaymentString = JOptionPane.showInputDialog
          (null, "Your payment does not cover the interest on your loan. "
                  + "Please enter a larger amount for your monthly payment.");
          monthlyPayment = Double.parseDouble(newMonthlyPaymentString);
            }
     
            double loanAmountRemaining = loanAmount;
     
            //begin second loop
            while (loanAmount != 0) {
     
            totalPaid += monthlyPayment;
     
            monthlyInterest = loanAmountRemaining * monthlyInterestRate;
     
            loanAmount += monthlyInterest;
     
            loanAmountRemaining = (loanAmount - totalPaid);
     
            month++;
     
            if (monthlyPayment > (loanAmountRemaining))
            {
                totalPaid += loanAmountRemaining;
     
                loanAmount = 0;
     
                month++;
            }
            }
     
            //display message that loan has been paid
            String.format("%.2f", totalPaid);
            JOptionPane.showMessageDialog
    (null,"Congradulations! You have paid off your loan. Total amount paid is $" 
                    + totalPaid);
    }
    }

    //display message that loan has been paid
    String.format("%.2f", totalPaid);
    JOptionPane.showMessageDialog
    (null,"Congradulations! You have paid off your loan. Total amount paid is $"
    + totalPaid);
    }
    }
    The last part is the part i want limited to 2 decimal places.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to round to 2 decimal places in a dialog box

    You aren't doing anything with the String returned by String.format("%.2f", totalPaid). Use it in the message you display.

    (Notice that String.format() does return something. It does not alter totalPaid in any way.)

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: How to round to 2 decimal places in a dialog box

    Try this :

    Math.round(number*100.0)/100.0;

    And also check this link:
    java - How to round the double value to 2 decimal points? - Stack Overflow
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: How to round to 2 decimal places in a dialog box

    You can use the DecimalFormat class to format your number.

    Line 3: Create an instance of your object and give it the parameters particular to your needs.
    Line 6. Call the method for formatting, which then produces a string expression.
    import java.text.DecimalFormat //Import Class
     
    DecimalFormat precision = new DecimalFormat( "0.00" ) //create instance of object
    int myNum = 5.753432;
     
    precision.format(myNum); //call method

    More on DecimalFormat Class: DecimalFormat

Similar Threads

  1. Making an input dialog box appear after closing a combo box
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 11th, 2012, 06:22 PM
  2. How do I get my answers to out with 2 decimal places?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 12:23 PM
  3. take a double, then round decimal to either to .0 or .5 only
    By MaxBodine in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 30th, 2011, 03:35 PM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM