等额本金
public class AverageCapitalUtils {
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;
}
public static BigDecimal getPerMonthPrincipal(BigDecimal invest, int totalMonth) {
BigDecimal monthIncome = invest.divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
return monthIncome;
}
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;
}
}
等额本息
public class AverageCapitalPlusInterestUtils {
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;
}
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;
}
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;
}
}