Java等额本息实现
⼯具类
import java.math.BigDecimal;
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).tScale(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).tScale(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).tScale(0, BigDecimal.ROUND_HALF_UP).longValue();
long perMonthPrincipalInterest =new BigDecimal(monthIncome).tScale(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).tScale(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 : Set()){
count = count + Value();
}
return count;
}
}
测试
public static void main(String[] args){
long invest =10000;// 本⾦
int month =12;
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+"⽉还款本⾦:"+ (i)+",还款利息:"+ (i));
invest = invest - (i);
}el{
System.out.println("等额本息---"+i+"⽉还款本⾦:"+invest +",还款利息:"+(perMonthPrincipalInterest - invest));
}
}
}
效果截图: