2014-09-26 1 views
-1

임씨는 수업을위한 대출 계산기를하고 있습니다. 나는 거의 끝났지 만 실종 된 유일한 방법은 $ 298.95833333333337 대신 INTEREST_RATE를 반올림하는 방법입니다. $ 298.96을 얻고 싶습니다. 하지만 어떻게해야할지 모르겠다. 이전 원리를 입력학교 프로젝트, 가장 가까운 센트로 두 배 증가.

=
현재 지불을 입력 =

이전 밸런스 : $ 25000.0
지불 : $ 450.0
유료 관심 : $ 151.04166666666666
원리 유료 : $ 298.95833333333337
신규 사용자 : $ 24701.041666666668

import java.util.Scanner; 

public class LoanCalculator { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    /** 
    * Declaration Section 
    * 
    */ 
    Scanner keyboard = new Scanner(System.in); 
    double INTEREST_RATE; 
    double currentPayment; 
    double oldPrincipal; 
    double interestPaid; 
    double principalPaid; 
    double newPrincipal; 


    /** 
    * Process Section 
    * 
    */ 

    System.out.println("Enter your old Principle = "); 
    oldPrincipal = keyboard.nextDouble(); 

    System.out.println("Enter your current payment = "); 
    currentPayment = keyboard.nextDouble(); 

    INTEREST_RATE = 7.25/100.0; //fix this 

    interestPaid = oldPrincipal * INTEREST_RATE/12; 

    principalPaid = currentPayment - interestPaid; 

    newPrincipal = oldPrincipal - principalPaid; 


    /** 
    * Output Section 
    * 
    * */ 
    System.out.println("Previous Balance: " + "$"+ oldPrincipal); 
    System.out.println("Payment: " + "$"+ currentPayment); 
    System.out.println("Interest Paid: " + "$"+ interestPaid); 
    System.out.println("Principle Paid: " + "$"+ principalPaid); 
    System.out.println("New Principal: " + "$"+ newPrincipal); 


}//Main() 

}//LoanCalculator 
+0

가능한 중복을 [ 자바에서 두 자릿수] (http://stackoverflow.com/questions/5710394/round-double-to-two-decimal-places-in-java) – Casey

+0

나는 가장 가까운 페니로 돌리고 싶다. 그리고 지금까지 우리는 BigDecimal에 관해서 배웠다. –

+0

@DacoApps cxdm이 질문 한 내용을 본 적이 있는가? BigDecimal은 학업 성향을 보이기 때문에 사례에서 큰 차이를 만들지 못할 수 있습니다. 검색 Fu에서 작업해야합니다. 혼자서도 답을 찾으려고 노력할 때 많은 시간을 소비 할 수 있습니다. – MarsAtomic

답변

-2

편집 : 위의 질문과 질문, 를 해결하지만, 다른 한편으로는 재정 계산을 위해 BigDecimal 클래스를 사용해야합니다으로
당신은 NumberFormat 클래스를 사용할 수 있습니다. BigDecimal 클래스는 포맷터가 내장되어 있으므로 추가합니다. 재무 계산에 대한

 public class LoanCalculator {public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    BigDecimal INTEREST_RATE, currentPayment, oldPrincipal, interestPaid, principalPaid, newPrincipal, month; 
    System.out.println("Enter your old Principle = "); 
    oldPrincipal = keyboard.nextBigDecimal(); 
    System.out.println("Enter your current payment = "); 
    currentPayment = keyboard.nextBigDecimal(); 
    NumberFormat formatter = new DecimalFormat("#0.00"); 
    INTEREST_RATE = new BigDecimal(7.25/100.0); 
    formatter.format(INTEREST_RATE);//only format 
    month = new BigDecimal(12); 
    interestPaid = INTEREST_RATE.multiply(oldPrincipal); 
    interestPaid = interestPaid.divide(month, 2); 
    principalPaid = currentPayment.subtract(interestPaid); 
    newPrincipal = oldPrincipal.subtract(principalPaid); 
    System.out.println("Previous Balance: " + "$" + oldPrincipal); 
    System.out.println("Payment: " + "$" + currentPayment); 
    System.out.println("Interest Paid: " + "$" + interestPaid.setScale(2, RoundingMode.HALF_UP) 
      + " $" + formatter.format(interestPaid)); 
    System.out.println("Principle Paid: " + "$" + principalPaid.setScale(2, RoundingMode.HALF_UP) 
      + " $" + formatter.format(principalPaid)); 
    System.out.println("New Principal: " + "$" + newPrincipal.setScale(2, RoundingMode.HALF_UP) 
      + " $" + formatter.format(newPrincipal));}} 
+1

정말 고마워요. –

+4

재무 계산에 복식을 사용하는 것은 사람들을 감옥에 던지게하는 것과 같습니다. – o11c

+1

여기에 설명이 없습니다. –

2

결코 사용 더블! 정말 위험합니다. Java 언어 사양에서

:

플로트 : 플로트 데이터 형식은 단 정밀도 32 비트 IEEE 754 부동 소수점입니다. 그 값의 범위는이 설명의 범위를 벗어나지 만, Java 언어 스펙의 부동 소수점 유형, 형식 및 값 섹션에서 지정됩니다. byte 및 short에 대한 권장 사항과 마찬가지로 큰 부동 소수점 배열에 메모리를 저장해야하는 경우 float (double 대신)를 사용하십시오. 이 데이터 유형은 통화와 같은 정확한 값에 사용해서는 안됩니다. 이를 위해서는 java.math.BigDecimal 클래스를 대신 사용해야합니다. 숫자와 문자열은 Java 플랫폼에서 제공하는 BigDecimal 및 기타 유용한 클래스를 다룹니다.

double : double 데이터 형식은 배정 밀도 64 비트 IEEE 754 부동 소수점 형식입니다. 그 값의 범위는이 설명의 범위를 벗어나지 만, Java 언어 스펙의 부동 소수점 유형, 형식 및 값 섹션에서 지정됩니다. 10 진수 값의 경우이 데이터 유형은 일반적으로 기본 선택입니다. 위에서 언급했듯이이 데이터 형식은 통화와 같은 정확한 값에 사용해서는 안됩니다.

을 사용하면 찾고있는 반올림 기능이 있습니다.

0

이 방법을 사용 완벽하고

String에 대한 가치와 Double 또는 Float

public String PerfectRound(String number, int limit){ 
//  System.out.println(numero); 
     String t="";int c=0,e=number.length(); 
     do {    
      try { 
       if(String.valueOf(number.charAt(c)).equals(".")){if(limit==0){break;}e=c+limit+1;} 
       t=t+number.charAt(c); 
      } catch (Exception je) { 
       t=t+"0"; 
      } 
      c++; 
//   System.out.println(t); 
     } while (c<e); 
//  System.out.println(t); 
     return t; 
    } 

예에 결과를 변환 확보 : 이중 원형의

double a=23.32313232; 
a=Double.valueOf(PerfectRound(a+"", 2)); 
System.out.println(a); 
+0

숫자는 반올림되지 않고 아래로만 반올림됩니다. –

관련 문제