等额本息计算

等额本息计算

设总贷款金额为P,月利率为a,总期次为m
设每月还款为X元,则有
第一个月还款剩余本金 P(1+a)-X
第二个月还款剩余本金 (P(1+a)-X)(1+a)-X=P(1+a)^2-X(1+(1+a))
第三个月还款剩余本金 ((P(1+a)-X)(1+a)-X)(1+a)-X
=(P(1+a)^2-X(1+(1+a)))(1+a)-X = P(1+a)^3-X(1+(1+a)+(1+a)^2)
...
第m个月还款剩余本金 P(1+a)^m-X(1+(1+a)+(1+a)^2+...+(1+a)^(m-1))
= P(1+a)^m - X ( (1+a)^(m-1) / (1+a-1) = P(1+a)^m - X ( ( (1+a)^m - 1 ) / a )
因为第m个月刚好还完钱,所以
P(1+a)^m - X ( ( (1+a)^m - 1 ) / a ) = 0
=> P(1+a)^m = X ( ( (1+a)^m - 1 ) / a )
=> X = P ((1+a)^m ) a / ( (1+a)^m - 1 )

以下为代码实现:

package com.jt;

import java.text.DecimalFormat;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class InterestCalc {

	public static JSONArray AverageCapitalPlusInterest(Double principal, Double interest ,int period ) {
		DecimalFormat dformat2 = new DecimalFormat("0.00");
		JSONArray  jsarray = new JSONArray();
		
		Double monthpay =  principal  *  interest * Math.pow((1+interest) , period)  / ( Math.pow((1+interest) , period) - 1 ) ;
		monthpay = Double.parseDouble( dformat2.format( monthpay) ); //锁定两位小数,精确到分

		Double totalLeftPrincipal = principal;
		Double totalInterest = 0.00;
		for(int i=0; i<period; i++) {
			JSONObject  jsOject = new JSONObject();
			if(i<period-1) {
				jsOject.put("total", dformat2.format( monthpay) ); //每期还款金额
				Double tempInterest = totalLeftPrincipal * interest; //当期利息
				tempInterest = Double.parseDouble( dformat2.format(tempInterest) );
				
				totalInterest = totalInterest + tempInterest;
				Double tempTotalLeftPrincipal = totalLeftPrincipal + tempInterest - monthpay ; //剩余本金
				Double currentPeriodPrincipal = monthpay - tempInterest ; //当期还款本金
				totalLeftPrincipal =  totalLeftPrincipal - currentPeriodPrincipal ; //剩余本金
				jsOject.put("principal", dformat2.format(currentPeriodPrincipal) ); 
				jsOject.put("interest", dformat2.format(tempInterest) );
				jsOject.put("leftPrincipal", dformat2.format(tempTotalLeftPrincipal) );
			}else {
				Double tempInterest = totalLeftPrincipal * interest; //当期利息
				tempInterest = Double.parseDouble( dformat2.format(tempInterest) );
				totalInterest = totalInterest + tempInterest;				
				Double total = Double.parseDouble(dformat2.format(tempInterest)) + Double.parseDouble(dformat2.format(totalLeftPrincipal));
				Double tempPrincipal = totalLeftPrincipal; //当期还款本金
				jsOject.put("total", total.toString() ); //最后一期剩余金额
				jsOject.put("principal", dformat2.format(tempPrincipal) ); 
				jsOject.put("interest", dformat2.format(tempInterest) );
				jsOject.put("leftPrincipal",0.00 ); //最后一期,剩余本金为0
			}
			jsarray.add(i, jsOject);
		}
		//总和
		JSONObject  jsOject = new JSONObject();
		jsOject.put("total", dformat2.format( principal + totalInterest ) ); //所有本金和利息的总金额
		jsOject.put("principal", dformat2.format( principal ) ); //所有的本金
		jsOject.put("interest", dformat2.format(totalInterest) );//所有的利息
		jsOject.put("leftPrincipal",dformat2.format(100*totalInterest/principal) ); //
		jsarray.add(period, jsOject);
		return jsarray;
	}
	
	public static void main(String args[]) {
		JSONArray  jsarrayResult = AverageCapitalPlusInterest(10000.00, 0.01, 12);
		DecimalFormat dformat = new DecimalFormat("0.00");
		for(int i=0; i<jsarrayResult.size(); i++) {
			JSONObject tempObject = JSONArray.parseObject(jsarrayResult.get(i).toString());
			System.out.print( dformat.format( tempObject.getDouble("total")  ) );
			System.out.print( "-" );
			System.out.print( dformat.format(  tempObject.getDouble("principal") ) );
			System.out.print( "-" );
			System.out.print( dformat.format(  tempObject.getDouble("interest") ) );
			System.out.print( "-" );
			System.out.println( dformat.format(  tempObject.getDouble("leftPrincipal") ) );
		}
	}
}

  

posted on 2023-03-28 18:09  子虚乌有  阅读(230)  评论(0编辑  收藏  举报