Java计算等额本金和等额本息

文章提供了两个Java类,分别用于计算等额本金和等额本息的还款计划。等额本金每月偿还固定的本金和递减的利息,而等额本息每月偿还的本金和利息总额固定,但本金与利息比例会变化。代码详细定义了计算每月还款额的方法,包括每月本金和利息的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java计算等额本金和等额本息

等额本金

/**
 * 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的还款本金额固定,
 * 而利息越来越少,借款人起初还款压力较大,但是随时间的推移每月还款数也越来越少。
 * @author: Tang
 * @date: 2023/2/2 14:22
 * @description:
 * @version: 1.0
 */
public class AverageCapitalUtils {
    /**
     * 等额本金计算获取还款方式为等额本金的每月偿还本金和利息
     * 公式:每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
     * @param invest 总借款额(贷款本金)
     * @param yearRate 年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
     */
    public static Map<Integer, BigDecimal> getPerMonthPrincipalInterest(BigDecimal invest, BigDecimal yearRate, int totalMonth) {
        // 每月本金
        BigDecimal monthPri = getPerMonthPrincipal(invest, totalMonth);
        //获取每月利息
        Map<Integer, BigDecimal> perMonthInterest = getPerMonthInterest(invest, yearRate, totalMonth);
        for (int i = 1; i <= totalMonth; i++) {
            BigDecimal interest = perMonthInterest.get(i);
            perMonthInterest.put(i,interest.add(monthPri));
        }
        return perMonthInterest;
    }


    /**
     * 等额本金计算获取还款方式为等额本金的每月偿还本金
     * @param invest 总借款额(贷款本金)
     * @param totalMonth 还款总月数
     * @return 每月本金
     */
    public static BigDecimal getPerMonthPrincipal(BigDecimal invest, int totalMonth) {
        BigDecimal monthIncome = invest.divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
        return monthIncome;
    }

    /**
     * 等额本金计算获取还款方式为等额本金的每月偿还本金和利息
     * 公式:每月偿还利息=(贷款本金-已归还本金累计额)×月利率
     * @param invest 总借款额(贷款本金)
     * @param yearRate 年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
     */
    public static Map<Integer, BigDecimal> getPerMonthInterest(BigDecimal invest, BigDecimal yearRate, int totalMonth) {
        Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>(totalMonth);
        // 每月本金
        BigDecimal monthPri = getPerMonthPrincipal(invest, totalMonth);
        // 获取月利率
        BigDecimal monthRate = yearRate.divide(BigDecimal.valueOf(12),10, BigDecimal.ROUND_DOWN);
        for (int i = 1; i <= totalMonth; i++) {
            BigDecimal subtract = invest.subtract(monthPri.multiply(BigDecimal.valueOf(i - 1)));
            BigDecimal monthInterest = subtract.multiply(monthRate).setScale(2, BigDecimal.ROUND_DOWN);
            map.put(i, monthInterest);
        }
        return map;
    }
}

等额本息

/**
 * @author: Tang
 * @date: 2023/2/2 14:18
 * @description:
 * @version: 1.0
 */
public class AverageCapitalPlusInterestUtils {

    /**
     * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
     * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数 -1〕
     * @param invest 总借款额(贷款本金)
     * @param yearRate 年利率
     * @param totalMonth  还款总月数
     * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
     */
    public static BigDecimal getPerMonthPrincipalInterest(BigDecimal invest, BigDecimal yearRate, Integer totalMonth) {
        BigDecimal monthRate = yearRate.divide(BigDecimal.valueOf(12),10,BigDecimal.ROUND_DOWN);
        BigDecimal pow = (monthRate.add(BigDecimal.valueOf(1))).pow(totalMonth);
        BigDecimal monthIncome = invest
            .multiply(monthRate).multiply(pow)
            .divide(pow.subtract(BigDecimal.valueOf(1)), 2, BigDecimal.ROUND_DOWN);
        return monthIncome;
    }

    /**
     * 等额本息计算获取还款方式为等额本息的每月偿还利息
     * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数 -1〕
     * @param invest 总借款额(贷款本金)
     * @param yearRate 年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还利息
     */
    public static Map<Integer, BigDecimal> getPerMonthInterest(BigDecimal invest, BigDecimal yearRate, Integer totalMonth) {
        Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
        BigDecimal monthRate = yearRate.divide(BigDecimal.valueOf(12),6,BigDecimal.ROUND_DOWN);
        BigDecimal monthInterest;
        for (int i = 1; i <= totalMonth ; i++) {
            BigDecimal multiply = invest.multiply(monthRate);
            BigDecimal pow = (monthRate.add(BigDecimal.valueOf(1))).pow(totalMonth);
            BigDecimal sub  = pow.subtract(monthRate.add(BigDecimal.valueOf(1)).pow(i-1));
            monthInterest = multiply.multiply(sub).divide(pow.subtract(BigDecimal.valueOf(1)), 6, BigDecimal.ROUND_DOWN);
            monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
            map.put(i, monthInterest);
        }
        return map;
    }
    /**
     * 等额本息计算获取还款方式为等额本息的每月偿还本金
     * @param invest 总借款额(贷款本金)
     * @param yearRate 年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还本金
     */
    public static Map<Integer, BigDecimal> getPerMonthPrincipal(BigDecimal invest, BigDecimal yearRate, Integer totalMonth) {
        BigDecimal monthIncome =  getPerMonthPrincipalInterest(invest,yearRate,totalMonth);
        Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);
        Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>(totalMonth);
        for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
            mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
        }
        return mapPrincipal;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值