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

• March 1st, 2013, 09:32 PM
JohnEliot
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.

Code Java:

```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.
• March 1st, 2013, 10:03 PM
pbrockway2
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.)
• March 2nd, 2013, 03:27 AM
Tamilarasi
Re: How to round to 2 decimal places in a dialog box
Try this :

Code :

`Math.round(number*100.0)/100.0;`

```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```