Java等额本息算法实现

5 篇文章 0 订阅

工具类:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 等额本息计算工具类
 *
 * <p>
 * 等额本息还款,也称定期付息,即借款人每月按相等的金额偿还贷款本息,其中每月贷款利息按月初剩余贷款本金计算并逐月结清。把按揭贷款的本金总额与利息总额相加,
 * 然后平均分摊到还款期限的每个月中。作为还款人,每个月还给银行固定金额,但每月还款额中的本金比重逐月递增、利息比重逐月递减。
 */
public class AverageCapitalPlusInterestUtils {

    /**
     * 每月偿还本金和利息
     * <p>
     * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕
     *
     * @param invest     总借款额(贷款本金,单位分)
     * @param yearRate   年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还本金和利息(入1 单位分)
     */
    public static long getPerMonthPrincipalInterest(long invest, double yearRate, int totalMonth) {
        double monthRate = yearRate / 12;
        double perMonthPrincipalInterest = invest * (monthRate * Math.pow(1 + monthRate, totalMonth))/(Math.pow(1 + monthRate, totalMonth) - 1);
        return new BigDecimal(perMonthPrincipalInterest).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
    }

    /**
     * 等额本息的每月偿还利息
     * <p>
     * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
     *
     * @param invest     总借款额(贷款本金,分)
     * @param yearRate   年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还利息(入1 单位分)
     */
    public static Map<Integer, Long> getPerMonthInterest(long invest, double yearRate, int totalMonth) {
        Map<Integer, Long> map = new HashMap<>();
        double monthRate = yearRate / 12;
        double monthInterest;
        for (int i = 1; i < totalMonth + 1; i++) {
            double multiply = invest * monthRate;
            double sub = Math.pow(1 + monthRate, totalMonth) - Math.pow(1 + monthRate, i - 1);
            monthInterest = multiply * sub/(Math.pow(1 + monthRate, totalMonth) - 1);
            map.put(i, new BigDecimal(monthInterest).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
        }
        return map;
    }

    /**
     * 等额本息的每月偿还本金(月还本息-月还利息)
     *
     * @param invest     总借款额(贷款本金,分)
     * @param yearRate   年利率
     * @param totalMonth 还款总月数
     * @return 每月偿还本金(取整舍 单位分)
     */
    public static Map<Integer, Long> getPerMonthPrincipal(long invest, double yearRate, int totalMonth) {
        double monthRate = yearRate / 12;
        double monthIncome = invest * monthRate * Math.pow(1 + monthRate, totalMonth)
                /(Math.pow(1 + monthRate, totalMonth) - 1);
        long perMonthPrincipalInterest = new BigDecimal(monthIncome).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();

        Map<Integer, Long> mapPrincipal = new HashMap<>();
        double monthInterest;
        for (int i = 1; i < totalMonth + 1; i++) {
            Double multiply = invest * monthRate;
            double sub = (Math.pow(1 + monthRate, totalMonth)) - (Math.pow(1 + monthRate, i - 1));
            monthInterest = multiply* sub/(Math.pow(1 + monthRate, totalMonth) - 1);
            long monthInterestL = new BigDecimal(monthInterest).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
            mapPrincipal.put(i, perMonthPrincipalInterest-monthInterestL);
        }
        return mapPrincipal;
    }

    /**
     * 等额本息的总利息
     *
     * @param invest     总借款额(贷款本金)
     * @param yearRate   年利率
     * @param totalMonth 还款总月数
     * @return 总利息 (单位分)
     */
    public static long getInterestCount(long invest, double yearRate, int totalMonth) {
        long count = 0;
        Map<Integer, Long> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);

        for (Map.Entry<Integer, Long> entry : mapInterest.entrySet()) {
            count = count + entry.getValue();
        }
        return count;
    }
 
}  

运行:

public static void main(String[] args) {
        long invest = 10000; // 本金
        int month = 6;
        double yearRate = 0.1; // 年利率
        long perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款本息:" + perMonthPrincipalInterest);
        Map<Integer, Long> mapInterest = getPerMonthInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款利息:" + mapInterest);
        Map<Integer, Long> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
        System.out.println("等额本息---每月还款本金:" + mapPrincipal);
        long count = getInterestCount(invest, yearRate, month);
        System.out.println("等额本息---总利息:" + count);

        for(int i=1;i<month + 1;i++){
            if(i<month){
                System.out.println("等额本息---"+i+"月还款本金:" + mapPrincipal.get(i) + ",还款利息:" + mapInterest.get(i));
                invest = invest - mapPrincipal.get(i);
            } else {
                System.out.println("等额本息---"+i+"月还款本金:" +invest + ",还款利息:" + (perMonthPrincipalInterest - invest));
            }

        }
    }

效果截图:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值