下面是使用Java代码实现商业贷款月供计算的例子。代码中使用了公式:
public class LoanCalculator {
public static void main(String[] args) {
// 贷款本金、贷款期限、年利率
calculateAndPrintEMI(545143.55, 27, 4.6);
calculateAndPrintEMI(545143.55, 27, 4.5);
System.out.println("=====提前还贷20万=====");
calculateAndPrintEMI(545143.55-200000, 27, 4.5);
}
/**
*
* @param loanAmount 贷款本金
* @param loanTermYears 贷款期限(年)
* @param interestRate 年利率(%)
*/
private static void calculateAndPrintEMI(double loanAmount, int loanTermYears, double interestRate) {
// 计算月利率
double monthlyInterestRate = interestRate / (12 * 100);
// 计算还款月数
int numPayments = loanTermYears * 12;
// 计算月供
double emi = calculateEMI(loanAmount, monthlyInterestRate, numPayments);
// 打印结果
System.out.printf("月供(%.2f%%利率): %.2f 元\n", interestRate, emi);
}
private static double calculateEMI(double loanAmount, double monthlyInterestRate, int numPayments) {
return loanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numPayments)) /
(Math.pow(1 + monthlyInterestRate, numPayments) - 1);
}
}
月供(4.60%利率): 2941.16 元
月供(4.50%利率): 2909.54 元
=====提前还贷20万=====
月供(4.50%利率): 1842.10 元