常见的贷款方式包括等额本金
以及等额本息
的贷款方式,如下代码中就包含了这两种贷款方式的计算
等额本息还款法:本金逐月递增,利息逐月递减,月还款数不变。
等额本金还款法:本金保持相同,利息逐月递减,月还款数递减。适合于有计划提前还贷。
import java.math.BigDecimal;
public class HousingLoanUtils {
//保留小数点后几位
static int point = 2;
//进位方式
static int inOrOut = BigDecimal.ROUND_HALF_UP;
public static void main(String[] args) {
//100万 30年 年利率0.0535 等额本金
evaluate(100_0000l,30,0.0535,1);
}
/**
*
* @param principal 贷款金额(元)
* @param years 还款时间(年)
* @param yearRate 年化利率
* @param type 1:等额本金 2:等额本息
*/
private static void evaluate(long principal, int years, double yearRate, int type) {
if(type == 1){
equalPrincipal(new BigDecimal(principal),years,new BigDecimal(yearRate));
}else if(type == 2){
equalPrincipalAndIntereset(new BigDecimal(principal),years,new BigDecimal(yearRate));
}
}
/**
* 等额本息
* @param principal 贷款金额(元)
* @param years 还款时间(年)
* @param yearRate 年化利率
*/
public static void equalPrincipalAndIntereset(BigDecimal principal,int years,BigDecimal yearRate){
int month = years * 12;
BigDecimal monthRate = yearRate.divide(new BigDecimal(12));
//(10000 * r) * (1 + r)^120 / [(1 + r)^120 - 1]
BigDecimal loanPerMonth = principal.multiply(monthRate).multiply(monthRate.add(new BigDecimal(1)).pow(month))
.divide(monthRate.add(new BigDecimal(1)).pow(month).subtract(new BigDecimal(1)),inOrOut);
System.out.print("【等额本息】月还款 = " + loanPerMonth.setScale(point,inOrOut));
BigDecimal allMoney = loanPerMonth.multiply(new BigDecimal(month));
System.out.print(" ,总还款 = " + allMoney.setScale(point,inOrOut));
BigDecimal subtract1 = allMoney.subtract(principal);
System.out.println(" ,总利息 = " + subtract1.setScale(point,inOrOut));
BigDecimal totalIntereset = new BigDecimal(0);
BigDecimal totalPrincipal = new BigDecimal(0);
//最大月=month+1
for (int i = 1; i <= 10; i++) {
BigDecimal currentIntereset = principal.multiply(monthRate).subtract(loanPerMonth)
.multiply(monthRate.add(new BigDecimal(1)).pow(i-1)).add(loanPerMonth);
totalIntereset = totalIntereset.add(currentIntereset);
totalPrincipal = totalPrincipal.add(loanPerMonth.subtract(currentIntereset));
System.out.print("【等额本息】第" + i + "月还的利息 = " + currentIntereset.setScale(point,inOrOut));
System.out.print(" ,第" + i + "月还的本金 = " + loanPerMonth.subtract(currentIntereset).setScale(point,inOrOut));
System.out.print(" ,第" + i + "月的总还款 = " + loanPerMonth.setScale(point,inOrOut));
System.out.print(" ,累计已还利息 = " + totalIntereset.setScale(point,inOrOut));
System.out.print(" ,累计已还本金 = " + totalPrincipal.setScale(point,inOrOut));
System.out.println();
}
}
/**
* 等额本金
* @param principal 贷款金额(元)
* @param years 还款时间(年)
* @param yearRate 年化利率
*/
public static void equalPrincipal(BigDecimal principal,int years,BigDecimal yearRate){
int month = years * 12;
BigDecimal monthRate = yearRate.divide(new BigDecimal(12));
//先计算首月应还,再算每月差值
//(10000 / month) + (m - 已经归还) * r
BigDecimal principalPerMonth = principal.divide(new BigDecimal(month).setScale(point,inOrOut),8,inOrOut);//每月还的本金(不变)
BigDecimal firstMonth = principalPerMonth.add(principal.multiply(monthRate));//首月还款额
BigDecimal subMoney = principalPerMonth.multiply(monthRate);//每月递减值
BigDecimal lastMonth = firstMonth.subtract(subMoney.multiply(new BigDecimal(month-1)));//尾月的还款额
BigDecimal allMoney = firstMonth.add(lastMonth).multiply(new BigDecimal(month)).divide(new BigDecimal(2));//总还款额
BigDecimal moneyPerMonth = allMoney.divide(new BigDecimal(month));//月均还款额
BigDecimal allIntereset = allMoney.subtract(principalPerMonth.multiply(new BigDecimal(month)));//总利息
System.out.print("【等额本金】月均还款 = " + moneyPerMonth.setScale(point, inOrOut));
System.out.print(" ,总还款 = " + allMoney.setScale(point,inOrOut));
System.out.println(" ,总利息 = " + allIntereset.setScale(point,inOrOut));
BigDecimal totalIntereset = new BigDecimal(0);
BigDecimal totalPrincipal = new BigDecimal(0);
//month
for (int i = 1; i <= 10; i++) {
BigDecimal currentMoney = firstMonth.subtract(subMoney.multiply(new BigDecimal(i - 1)));//当月还款额
BigDecimal currentIntereset = currentMoney.subtract(principalPerMonth);//当月利息
totalIntereset = totalIntereset.add(currentIntereset);
totalPrincipal = totalPrincipal.add(currentMoney.subtract(currentIntereset));
System.out.print("【等额本金】第" + i + "月还的利息 = " + currentIntereset.setScale(point,inOrOut));
System.out.print(" ,第" + i + "月还的本金 = " + principalPerMonth.setScale(point,inOrOut));
System.out.print(" ,第" + i + "月的总还款 = " + currentMoney.setScale(point,inOrOut));
System.out.print(" ,累计已还利息 = " + totalIntereset.setScale(point,inOrOut));
System.out.println(" ,累计已还本金 = " + totalPrincipal.setScale(point,inOrOut));
}
}
}
等额本金的贷款打印
【等额本金】月均还款 = 5013.14 ,总还款 = 1804729.17 ,总利息 = 804729.17
【等额本金】第1月还的利息 = 4458.33 ,第1月还的本金 = 2777.78 ,第1月的总还款 = 7236.11 ,累计已还利息 = 4458.33 ,累计已还本金 = 2777.78
【等额本金】第2月还的利息 = 4445.95 ,第2月还的本金 = 2777.78 ,第2月的总还款 = 7223.73 ,累计已还利息 = 8904.28 ,累计已还本金 = 5555.56
【等额本金】第3月还的利息 = 4433.56 ,第3月还的本金 = 2777.78 ,第3月的总还款 = 7211.34 ,累计已还利息 = 13337.85 ,累计已还本金 = 8333.33
【等额本金】第4月还的利息 = 4421.18 ,第4月还的本金 = 2777.78 ,第4月的总还款 = 7198.96 ,累计已还利息 = 17759.03 ,累计已还本金 = 11111.11
【等额本金】第5月还的利息 = 4408.80 ,第5月还的本金 = 2777.78 ,第5月的总还款 = 7186.57 ,累计已还利息 = 22167.82 ,累计已还本金 = 13888.89
【等额本金】第6月还的利息 = 4396.41 ,第6月还的本金 = 2777.78 ,第6月的总还款 = 7174.19 ,累计已还利息 = 26564.24 ,累计已还本金 = 16666.67
【等额本金】第7月还的利息 = 4384.03 ,第7月还的本金 = 2777.78 ,第7月的总还款 = 7161.81 ,累计已还利息 = 30948.26 ,累计已还本金 = 19444.44
【等额本金】第8月还的利息 = 4371.64 ,第8月还的本金 = 2777.78 ,第8月的总还款 = 7149.42 ,累计已还利息 = 35319.91 ,累计已还本金 = 22222.22
【等额本金】第9月还的利息 = 4359.26 ,第9月还的本金 = 2777.78 ,第9月的总还款 = 7137.04 ,累计已还利息 = 39679.17 ,累计已还本金 = 25000.00
【等额本金】第10月还的利息 = 4346.87 ,第10月还的本金 = 2777.78 ,第10月的总还款 = 7124.65 ,累计已还利息 = 44026.04 ,累计已还本金 = 27777.78
等额本息的贷款打印
【等额本息】月还款 = 5584.14 ,总还款 = 2010289.30 ,总利息 = 1010289.30
【等额本息】第1月还的利息 = 4458.33 ,第1月还的本金 = 1125.80 ,第1月的总还款 = 5584.14 ,累计已还利息 = 4458.33 ,累计已还本金 = 1125.80
【等额本息】第2月还的利息 = 4453.31 ,第2月还的本金 = 1130.82 ,第2月的总还款 = 5584.14 ,累计已还利息 = 8911.65 ,累计已还本金 = 2256.63
【等额本息】第3月还的利息 = 4448.27 ,第3月还的本金 = 1135.86 ,第3月的总还款 = 5584.14 ,累计已还利息 = 13359.92 ,累计已还本金 = 3392.49
【等额本息】第4月还的利息 = 4443.21 ,第4月还的本金 = 1140.93 ,第4月的总还款 = 5584.14 ,累计已还利息 = 17803.13 ,累计已还本金 = 4533.42
【等额本息】第5月还的利息 = 4438.12 ,第5月还的本金 = 1146.02 ,第5月的总还款 = 5584.14 ,累计已还利息 = 22241.25 ,累计已还本金 = 5679.43
【等额本息】第6月还的利息 = 4433.01 ,第6月还的本金 = 1151.12 ,第6月的总还款 = 5584.14 ,累计已还利息 = 26674.26 ,累计已还本金 = 6830.56
【等额本息】第7月还的利息 = 4427.88 ,第7月还的本金 = 1156.26 ,第7月的总还款 = 5584.14 ,累计已还利息 = 31102.14 ,累计已还本金 = 7986.82
【等额本息】第8月还的利息 = 4422.73 ,第8月还的本金 = 1161.41 ,第8月的总还款 = 5584.14 ,累计已还利息 = 35524.87 ,累计已还本金 = 9148.23
【等额本息】第9月还的利息 = 4417.55 ,第9月还的本金 = 1166.59 ,第9月的总还款 = 5584.14 ,累计已还利息 = 39942.42 ,累计已还本金 = 10314.82
【等额本息】第10月还的利息 = 4412.35 ,第10月还的本金 = 1171.79 ,第10月的总还款 = 5584.14 ,累计已还利息 = 44354.76 ,累计已还本金 = 11486.61