等额本金&等额本息工具类2023

等额本金&等额本息工具类2023

等额本金

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

/**
 * 等额本金工具类
 * <p>
 * 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的还款本金额固定,
 * 而利息越来越少,借款人起初还款压力较大,但是随时间的推移每月还款数也越来越少。
 *
 * @author xl.niu
 * @since 2023/6/5
 */
public class AverageCapitalUtils {

	/**
	 * 等额本金的每月偿还本金和利息
	 * <p>
	 * 公式:每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param yearRate   年利率
	 * @param totalMonth 还款总月数
	 * @return 每月偿还本金和利息, 不四舍五入,直接截取小数点最后两位
	 */
	public static Map<Integer, BigDecimal> getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
		Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
		// 每月本金
		BigDecimal monthPri = getPerMonthPrincipal(invest, totalMonth);
		// 每月利息
		Map<Integer, BigDecimal> monthInterests = getPerMonthInterest(invest, yearRate, totalMonth);
		for (Map.Entry<Integer, BigDecimal> entry : monthInterests.entrySet()) {
			map.put(entry.getKey(), entry.getValue().add(monthPri).setScale(2, RoundingMode.CEILING));
		}
		return map;
	}

	/**
	 * 等额本金的每月偿还利息
	 * <p>
	 * 公式:每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param yearRate   年利率
	 * @param totalMonth 还款总月数
	 * @return 每月偿还利息
	 */
	public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
		Map<Integer, BigDecimal> inMap = new HashMap<Integer, BigDecimal>();
		BigDecimal monthPrincipal = getPerMonthPrincipal(invest, totalMonth);//月偿还本金
		double monthRate = yearRate / 12;
		BigDecimal monthRateBigDecimal = new BigDecimal(String.valueOf(monthRate));//月利率
		for (int i = 1; i <= totalMonth; i++) {
			BigDecimal investBigDecimal = new BigDecimal(String.valueOf(invest));//本金
			BigDecimal finishPrincipal = monthPrincipal.multiply(new BigDecimal((i - 1)));//已还本金合计

			BigDecimal unfinish = investBigDecimal.subtract(finishPrincipal);

			BigDecimal monthInterest = unfinish.multiply(monthRateBigDecimal);
			inMap.put(i, monthInterest);
		}
		return inMap;
	}

	/**
	 * 等额本金的每月偿还本金
	 * <p>
	 * 公式:每月应还本金=贷款本金÷还款月数
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param totalMonth 还款总月数
	 * @return 每月偿还本金
	 */
	public static BigDecimal getPerMonthPrincipal(double invest, int totalMonth) {
		return new BigDecimal(invest).divide(new BigDecimal(totalMonth), 4, BigDecimal.ROUND_DOWN);
	}

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

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

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double invest = 20000; // 本金
		double yearRate = 0.15; // 年利率
		int month = 12;

		BigDecimal principal = getPerMonthPrincipal(invest, month);//月偿还本金
		Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);//月偿还利息
		Map<Integer, BigDecimal> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);//月还款合计

		System.out.println("序号\t利息\t\t本金\t\t每月还款本息");
		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			System.out.println(entry.getKey() + "\t" + entry.getValue().setScale(2, BigDecimal.ROUND_HALF_UP) + "\t" + principal.setScale(2, RoundingMode.CEILING) + "\t" + getPerMonthPrincipalInterest.get(entry.getKey()));
		}

		BigDecimal count = getInterestCount(invest, yearRate, month);
		System.out.println("等额本金---总利息:" + count.setScale(2, BigDecimal.ROUND_HALF_UP));
	}
}

等额本金


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

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

	/**
	 * 等额本息的每月偿还本金和利息
	 * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param yearRate   年利率
	 * @param totalmonth 还款总月数
	 * @return 每月偿还本金和利息, 四舍五入
	 */
	public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
		double monthRate = yearRate / 12;
		BigDecimal monthIncome = new BigDecimal(invest)
			.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
			.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_HALF_UP);
		return monthIncome.doubleValue();
	}

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

	/**
	 * 等额本息的每月偿还本金
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param yearRate   年利率
	 * @param totalmonth 还款总月数
	 * @return 每月偿还本金
	 */
	public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
		double monthRate = yearRate / 12;
		BigDecimal monthIncome = new BigDecimal(invest)
			.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
			.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_HALF_UP);
		Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
		Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();

		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
		}
		return mapPrincipal;
	}

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

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

	/**
	 * 应还总和
	 *
	 * @param invest     总借款额(贷款本金)
	 * @param yearRate   年利率
	 * @param totalmonth 还款总月数
	 * @return 应还本金总和
	 */
	public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
		double monthRate = yearRate / 12;
		BigDecimal perMonthInterest = new BigDecimal(invest)
			.multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
			.divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_HALF_UP);
		BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalmonth));
		count = count.setScale(2, BigDecimal.ROUND_HALF_UP);
		return count.doubleValue();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		double invest = 20000; // 本金
		double yearRate = 0.15; // 年利率
		int month = 12;
		double perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);//每月还款本息
		Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);//每月还款利息
		Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);//每月还款本金


		System.out.println("序号\t利息\t\t本金\t\t每月还款本息");
		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			System.out.println(entry.getKey() + "\t" + entry.getValue() + "\t" + mapPrincipal.get(entry.getKey()) + "\t" + perMonthPrincipalInterest);
		}

		double count = getInterestCount(invest, yearRate, month);
		System.out.println("等额本息---总利息:" + count);
		double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
		System.out.println("等额本息---应还本息总和:" + principalInterestCount);
	}
}

问题反馈

请添加图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值