title: Rounding a number with printf in java?
body: <p>i am trying to use System.out.printf to round a very large number to 2 decimal places. This is the code i am using:</p>
  
  <pre><code>System.out.printf(" %1.2f = overpayment:$" + overpayment);
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <pre><code>Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%1.2f'
      at java.util.Formatter.format(Unknown Source)
      at java.io.PrintStream.format(Unknown Source)
  </code></pre>
  
  <p>I am under the impression that %f is the format specifier and %1.2f is used for a floating point number with 2 digits after the decimal.
  I am trying to round 4.4260494195128784E-4 to 4.43. Thank you </p>
  
  <p>Since I am getting under a cent for my overpayment, I think i have the wrong formula to calculate it. Does anyone have an idea on how to attain overpayment of a loan? The overpayment value should be 4.43. Here is my code:</p>
  
  <pre><code>import java.util.Scanner;
  </code></pre>
  
  <p>public class CreditCardPayoff {</p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      double principle;
      double annualInterestRate;
      double monthlyPayment;
      double numerator;
      double denominator;
      double monthsToPayOff;
      double monthsToPayOffCeiled;
      double totalAmountPaid;
      double totalInterestPaid;
      double overpayment;
  
      Scanner keyboard = new Scanner(System.in);
      principle = keyboard.nextDouble();
      annualInterestRate = keyboard.nextDouble();
      monthlyPayment = keyboard.nextDouble();
  
      numerator = Math.log(monthlyPayment) - Math.log(monthlyPayment-
              (annualInterestRate / 1200.00) * principle);
      denominator = Math.log((annualInterestRate/1200.00) + 1.0);
              monthsToPayOff = numerator/denominator;
  
      monthsToPayOffCeiled = Math.ceil(monthsToPayOff);
  
      totalAmountPaid = monthsToPayOffCeiled * monthlyPayment;
  
      totalInterestPaid = totalAmountPaid - principle;
  
  
      overpayment = (monthsToPayOffCeiled - monthsToPayOff)/monthlyPayment;
  
  
  
  
      System.out.println("Principle:" + principle);
      System.out.println("Annual Interest Rate:" + annualInterestRate);
      System.out.println("Monthly Payment:" + monthlyPayment);
  
      System.out.println("Months Needed To Pay Off:" + (int)monthsToPayOffCeiled);
      System.out.println("Total Amount Paid: $" + totalAmountPaid);
      System.out.println("Total Interest Paid: $" + totalInterestPaid);
      System.out.printf("overpayment: $%1.2f ", overpayment);
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My method to calculate overpayment, obtain difference between monthsToPayOff and monthsToPayOffCeil (ceiling) then divide by monthly payment. This got me 4.4260494195128784E-4.</p>