title: Java Bigdecimal class
body: <p>This is a homework assignment and I am trying to program an interest calculator. I am required to use the BigDecimal class but cannot figure out how to make the output in currency or percent. I am not exactly sure what questions to ask but I am going to post both codes the BigDecimal class code and the other which displays the output the way I need it but is not using BigDecimal. Any suggestions appreciated.</p>
  
  <pre><code>   import java.util.Scanner;
  
     import java.math.*;
  
     public class project3a 
     {
  
  
           public static void main(String[] args)
           {   
              System.out.println("Welcome to the interest calculator");   
              System.out.println();
  
  
              // create a scanner object and start while loop             
              Scanner sc = new Scanner(System.in);
              String choice ="y";
              while (choice.equalsIgnoreCase("y"))
             {
  
               //Get input from user
               System.out.print("Enter Loan amount: ");
               double Loanamount = sc.nextDouble();
  
               System.out.print("Enter Interest Rate: ");
               double interestrate = sc.nextDouble();
  
               //calculate results    
               double Interest = Loanamount * interestrate;
  
              //format 
              BigDecimal decimalLoanamount = new BigDecimal(Double.toString (Loanamount));
              decimalLoanamount = decimalLoanamount.setScale(2, RoundingMode.HALF_UP);
              BigDecimal decimalinterestrate = new BigDecimal(Double.toString(interestrate));
              BigDecimal decimalInterest = new BigDecimal (Double.toString(Interest));
              decimalInterest = decimalInterest.setScale(2,RoundingMode.HALF_UP);
  
              //Display results
              System.out.println("Loan amount: " + decimalLoanamount);
              System.out.println("Interest rate: " + decimalinterestrate);
              System.out.println("Interest:" + decimalInterest);  
              System.out.println();
  
              //see if user wants to continue             
              System.out.print("Continue? (y/n): ");
              choice = sc.next();
              System.out.println();
  
  
           }
  
      }
  
      }
  </code></pre>
  
  <hr>
  
  <pre><code>    import java.util.Scanner;
      import java.text.NumberFormat;
      import java.math.*;
  
      public class project3a 
      {
  
  
      public static void main(String[] args)
      {   
          System.out.println("Welcome to the interest calculator");   
          System.out.println();
  
  
          // create a scanner object and start while loop             
          Scanner sc = new Scanner(System.in);
          String choice ="y";
          while (choice.equalsIgnoreCase("y"))
          {
  
              //Get input from user
              System.out.print("Enter Loan amount: ");
              double Loanamount = sc.nextDouble();
  
              System.out.print("Enter Interest Rate: ");
              double interestrate = sc.nextDouble();
  
              //calculate results 
              double Interest = Loanamount * interestrate;
  
              //format and display results
              NumberFormat currency = NumberFormat.getCurrencyInstance();
              NumberFormat percent = NumberFormat.getPercentInstance();
              String message =
                  "Loan amount:   " + currency.format (Loanamount) + "\n"
                + "Interest rate: " + percent.format (interestrate)+ "\n"
                + "Interest:      " + currency.format (Interest) + "\n";
  
              System.out.println(message);
  
              //see if user wants to continue             
              System.out.print("Continue? (y/n): ");
              choice = sc.next();
              System.out.println();
  
  
           }
  
      }
  
      }
  </code></pre>