How do I format my results with DecimalFormat if I'm using a separate class?

So I'm very new to Java, and I'm currently trying to create a driver class that will use my Mortgage class to calculate a mortgage. I have everything working except that my output does not format the results to two decimal places (0.00), even though I have the code there. Here is a piece of my driver code:

Mortgage mortgage1 = new Mortgage(annualInterestRate, numOfYears, loanAmount);

  DecimalFormat df = new DecimalFormat("0.00");

  JOptionPane.showMessageDialog(null, "Your Monthly Payment is: " 
  + mortgage1.monthlyPayment() + "\n" + "Your Total Payment is: " + 
  mortgage1.totalPayment());

It works perfectly fine, and outputs the correct numbers, but doesn't format them correctly. Thank you for your help.

Jon Skeet
people
quotationmark

Well you're not actually using df at all. You need to pass the result of calling monthlyPayment() and totalPayment() to the format method. For example:

Mortgage mortgage1 = new Mortgage(annualInterestRate, numOfYears, loanAmount);
DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null,
    "Your Monthly Payment is: " 
    + df.format(mortgage1.monthlyPayment()) + "\n"
    + "Your Total Payment is: "
    + df.format(mortgage1.totalPayment()));

people

See more on this question at Stackoverflow