2012-02-10 86 views
1

这是一项家庭作业,我正在设计一个兴趣计算器。我需要使用BigDecimal类,但无法弄清楚如何以货币或百分比形式输出。我不完全确定要问什么问题,但我要发布两个代码BigDecimal类代码,另一个以我需要的方式显示输出,但不使用BigDecimal。任何建议感激。Java Bigdecimal类

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(); 


     } 

    } 

    } 

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(); 


     } 

    } 

    } 
+3

代码越少,问题越好,答案越快越好。 – 2012-02-10 02:40:07

+2

我只是试图表明,我正在努力,而不是只是为了寻找答案,因为这是作业。 – 2012-02-10 02:52:01

+1

您能更具体地了解您期望的输出和您获得什么? 'currency.format()'和'percent.format()'是你的问题吗? – 2012-02-10 02:58:02

回答

0

BigDecimal有方法doubleValue(),你可以在NumberFormat使用。

BigDecimal bd = new BigDecimal("15.25"); 
NumberFormat currency = NumberFormat.getCurrencyInstance(); 
NumberFormat percent = NumberFormat.getPercentInstance(); 

System.out.println(currency.format(bd.doubleValue())); //In Brazil outputs R$ 15,25 
System.out.println(percent.format(bd.doubleValue())); // 1.525% 
+1

如果要使用非浮点表示形式来避免浮动错误,则不应在最后通过转换为double来重新引入浮点表示形式错误。 http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html说:“因此,你不能依靠NumericFormat产生非常大的数字(大约13位或更多数字)的准确结果。 “ – 2012-02-10 03:40:14

+0

@MikeSamuel感谢您的链接,有趣的一点! – 2012-02-10 03:48:03

+1

['BigDecimal.format(Object,...)'](http://docs.oracle.com/javase/1.5.0/docs/api/java/text/DecimalFormat.html#format%28java.lang。 Object,%20java.lang.StringBuffer,%20java.text.FieldPosition%29)并没有将'BigDecimal'转换为'NumberFormat'的方式。 – 2012-02-10 03:53:00