2013-07-09 1 views
0

나는 수업을위한 간단한 대출 계산기를 쓰고있다. 금액을 달러 형태로 반환하려고합니다. 나는 system.out.printf()를 발견했다. 그러나 제대로 작동하는 것처럼 보일 수는 없습니다. 여기 자바, 달러 값으로 두 배 형식

는 현재 코드입니다 : (나는 또한 제대로 작동하지 않습니다 알고 나중에 해결됩니다)

public static void calculate() { 

    annualInterest = (annualInterest * 0.01)/12; //percentage to decimal then to month interest % 
    term = term * 12; 
    double payment = amtBorrowed * annualInterest; 

    double temp = 1/(1 + annualInterest); 
    temp = Math.pow(temp,term); 
    temp = 1 - temp; 

    payment = payment/temp; 
    double Interest = amtBorrowed * annualInterest; 
    double principal = payment - Interest; 

    System.out.println("Your monthly payment is " + payment); 
    System.out.println("   | Interest | principal"); 
    System.out.println("Month 1 :" +" "+ Interest + " " + principal); 
    for (int i = 2; i < term + 1; i ++){ 
     System.out.print("Month "+ i + " :"); 
     amtBorrowed = amtBorrowed - (Interest + principal); 
     payment = amtBorrowed * annualInterest; 
     temp = 1/(1 + annualInterest); 
     temp = Math.pow(temp,term); 
     temp = 1 - temp; 
     payment = payment/temp; 
     Interest = amtBorrowed * annualInterest; 
     principal = payment - Interest; 
     System.out.println(" " + Interest + " " + principal); 
    } 
} 

출력 :

Your monthly payment is 4432.061025275802 
      | Interest | principal 
Month 1 : 500.0 3932.0610252758024 
Month 2 : 477.839694873621 3757.789681084494 
Month 3 : 456.6615479938304 3591.242149217312 
Month 4 : 436.4220295077747 3432.0761055985745 
Month 5 : 417.079538832243 3279.9643981645363 
Month 6 : 398.5943191472591 3134.594374430564 
+0

을 도움이된다면 http://vanillajava.blogspot.co.uk/2011/08/double-your-money- again.html –

+0

두 번 사용하지 않습니까? 당신이 사용하지 않는 것을 의미합니까? 아픈 기사를 읽어 주셔서 감사합니다! – andrsnn

+0

"double"을 사용하면 안되는 이유에 대해 누군가 의견을 말했지만 동의하지 않습니다. 대부분의 투자 은행은 BigDecimal이 아닌 long 또는 double을 사용합니다 (특히 C++에서는 esp) –

답변

3

는 printf와 대신

System.out.printf("Your monthly payment is $%.2f%n", payment); 
사용

이것은 인쇄되어야합니다.

Your monthly payment is $4432.06 

또한

System.out.printf(" %8.2f %8.2f%n", Interest, principal); 
+0

멋지게 printf로 실험 해 보겠습니다. 감사합니다! – andrsnn

+0

또한 왜 8 %? 8.2? 왜 첫 번째 예제가 아닌가? – andrsnn

+0

@ user2210274 '8'은 숫자를 제외하고 열이 정렬됩니다. 즉 최소 너비가 8입니다. 첫 번째 예에서는 $와 숫자 사이에 많은 공백이 필요하지 않을 수 있습니다. –

1

확인 당신이 통화를 두 번 사용해야하는 이유이 DecimalFormat.getCurrencyInstance(Locale.US).format(doubleValue)