好了,所以我有這個類:貸款利率返還錯誤價值?
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。我究竟做錯了什麼?
看到你正在使用的雙打作爲輸入變量(在BigDecimal的代替,你應該用錢的時候!)它可能是與你在你的計算中有1秒。你有沒有試過把它全部做成1.0?我不知道優先規則的心臟,也許你不要:) – extraneon
你應該不是利用interestRate進行除法運算之前從1減去,而不是相反? –
+1使用BigDecimal http://www.javapractices.com/topic/TopicAction.do?Id=213 –