贷款利率回归错误价值?

时间:2013-09-21 18:26:59

标签: java

好吧,我有这个班:

package com.sandrovictoriaarena.unittesting;

public class LoanCalculator {
private double pricipelValue; //PV
private double interestRate; //rate
private int loanLength; //n
private double result;

public LoanCalculator(double pricipelValue, double interestRate,
        int loanLength) {
    super();
    this.pricipelValue = pricipelValue;
    this.interestRate = interestRate / 100;
    this.loanLength = loanLength;
}

public double getPricipelValue() {
    return pricipelValue;
}

public void setPricipelValue(double pricipelValue) {
    this.pricipelValue = pricipelValue;
}

public double getInterestRate() {
    return interestRate;
}

public void setInterestRate(double interestRate) {
    this.interestRate = interestRate;
}

public int getLoanLength() {
    return loanLength;
}

public void setLoanLength(int loanLength) {
    this.loanLength = loanLength;
}

@Override
public String toString() {
    return "LoanCalculator [pricipelValue=" + 
    pricipelValue + ", interestRate=" + interestRate + 
    ", loanLength=" + loanLength + "]";
}

public double calculateMonthlyPayment() throws IllegalArgumentException{
    result = (1 - (Math.pow((1 + interestRate), -1 * loanLength)));
    result = interestRate / result;
    result = result * pricipelValue;
    return result;
}

}

我正在制作一个具有以下值的对象: 新的LoanCalculator(100.0,20.0,6);

当我运行calculateMonthlyPayment()时的结果应该是17.65,但我一直得到30.07。我做错了什么?

1 个答案:

答案 0 :(得分:3)

代码和公式都是正确的。

您的利率为20%,但可能是每年20%。我认为你的间隔是6,这可能是6个月。您应该始终以与间隔数相同的条件传递利率。在这里,您的间隔数以月为单位,但您的利率以年(年)为单位。因此,只需将利率作为月费率,您应该得到正确的答案!

每月利率为20%/ 12 =(0.2 / 12)。如果您在输入中替换它,您将得到正确的答案。所以你应该这样做:new LoanCalculator (100.0,20.0/12,6)