等额本息计算
等额本息计算
设总贷款金额为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 )
以下为代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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" ) ) ); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-03-28 逆向工程核心原理之第23章之dll注入注意点
2022-03-28 逆向工程核心原理之第23章之注册表注入