等额本金、等额本息工具类(Java版)

等额本息:


   
   
  1. /**
  2. * Description:等额本息工具类
  3. * Copyright: Copyright (corporation)2015
  4. * Company: Corporation
  5. * @author: 凯文加内特
  6. * @version: 1.0
  7. * Created at: 2015年11月30日 下午3:45:46
  8. * Modification History:
  9. * Modified by :
  10. */
  11. package com.utils;
  12. import java.math.BigDecimal;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * 等额本息还款,也称定期付息,即借款人每月按相等的金额偿还贷款本息,其中每月贷款利息按月初剩余贷款本金计算并逐月结清。把按揭贷款的本金总额与利息总额相加,
  17. * 然后平均分摊到还款期限的每个月中。作为还款人,每个月还给银行固定金额,但每月还款额中的本金比重逐月递增、利息比重逐月递减。
  18. */
  19. public class AverageCapitalPlusInterestUtils {
  20. /**
  21. * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息
  22. *
  23. * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕
  24. *
  25. * @param invest
  26. * 总借款额(贷款本金)
  27. * @param yearRate
  28. * 年利率
  29. * @param month
  30. * 还款总月数
  31. * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
  32. */
  33. public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
  34. double monthRate = yearRate / 12;
  35. BigDecimal monthIncome = new BigDecimal(invest)
  36. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  37. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  38. return monthIncome.doubleValue();
  39. }
  40. /**
  41. * 等额本息计算获取还款方式为等额本息的每月偿还利息
  42. *
  43. * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
  44. * @param invest
  45. * 总借款额(贷款本金)
  46. * @param yearRate
  47. * 年利率
  48. * @param month
  49. * 还款总月数
  50. * @return 每月偿还利息
  51. */
  52. public static Map <Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) {
  53. Map <Integer, BigDecimal> map = new HashMap <Integer, BigDecimal>();
  54. double monthRate = yearRate/12;
  55. BigDecimal monthInterest;
  56. for (int i = 1; i < totalmonth + 1; i++) {
  57. BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
  58. BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth)).subtract(new BigDecimal(Math.pow(1 + monthRate, i-1)));
  59. monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 6, BigDecimal.ROUND_DOWN);
  60. monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
  61. map.put(i, monthInterest);
  62. }
  63. return map;
  64. }
  65. /**
  66. * 等额本息计算获取还款方式为等额本息的每月偿还本金
  67. *
  68. * @param invest
  69. * 总借款额(贷款本金)
  70. * @param yearRate
  71. * 年利率
  72. * @param month
  73. * 还款总月数
  74. * @return 每月偿还本金
  75. */
  76. public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
  77. double monthRate = yearRate / 12;
  78. BigDecimal monthIncome = new BigDecimal(invest)
  79. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  80. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  81. Map <Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
  82. Map <Integer, BigDecimal> mapPrincipal = new HashMap <Integer, BigDecimal>();
  83. for (Map.Entry <Integer, BigDecimal> entry : mapInterest.entrySet()) {
  84. mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
  85. }
  86. return mapPrincipal;
  87. }
  88. /**
  89. * 等额本息计算获取还款方式为等额本息的总利息
  90. *
  91. * @param invest
  92. * 总借款额(贷款本金)
  93. * @param yearRate
  94. * 年利率
  95. * @param month
  96. * 还款总月数
  97. * @return 总利息
  98. */
  99. public static double getInterestCount(double invest, double yearRate, int totalmonth) {
  100. BigDecimal count = new BigDecimal(0);
  101. Map <Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
  102. for (Map.Entry <Integer, BigDecimal> entry : mapInterest.entrySet()) {
  103. count = count.add(entry.getValue());
  104. }
  105. return count.doubleValue();
  106. }
  107. /**
  108. * 应还本金总和
  109. * @param invest
  110. * 总借款额(贷款本金)
  111. * @param yearRate
  112. * 年利率
  113. * @param month
  114. * 还款总月数
  115. * @return 应还本金总和
  116. */
  117. public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
  118. double monthRate = yearRate / 12;
  119. BigDecimal perMonthInterest = new BigDecimal(invest)
  120. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  121. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  122. BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalmonth));
  123. count = count.setScale(2, BigDecimal.ROUND_DOWN);
  124. return count.doubleValue();
  125. }
  126. /**
  127. * @param args
  128. */
  129. public static void main(String[] args) {
  130. double invest = 20000; // 本金
  131. int month = 12;
  132. double yearRate = 0.15; // 年利率
  133. double perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
  134. System.out.println(“等额本息—每月还款本息:” + perMonthPrincipalInterest);
  135. Map <Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
  136. System.out.println(“等额本息—每月还款利息:” + mapInterest);
  137. Map <Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
  138. System.out.println(“等额本息—每月还款本金:” + mapPrincipal);
  139. double count = getInterestCount(invest, yearRate, month);
  140. System.out.println(“等额本息—总利息:” + count);
  141. double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
  142. System.out.println(“等额本息—应还本息总和:” + principalInterestCount);
  143. }
  144. }

等额本金:


   
   
  1. /**
  2. * Description:等额本金工具类
  3. * Copyright: Copyright (Corporation)2015
  4. * Company: Corporation
  5. * @author: 凯文加内特
  6. * @version: 1.0
  7. * Created at: 2015年12月1日 上午8:38:23
  8. * Modification History:
  9. * Modified by :
  10. */
  11. package com.utils;
  12. import java.math.BigDecimal;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的还款本金额固定,
  17. * 而利息越来越少,借款人起初还款压力较大,但是随时间的推移每月还款数也越来越少。
  18. */
  19. public class AverageCapitalUtils {
  20. /**
  21. * 等额本金计算获取还款方式为等额本金的每月偿还本金和利息
  22. *
  23. * 公式:每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
  24. *
  25. * @param invest
  26. * 总借款额(贷款本金)
  27. * @param yearRate
  28. * 年利率
  29. * @param month
  30. * 还款总月数
  31. * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位
  32. */
  33. public static Map <Integer, Double> getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
  34. Map <Integer, Double> map = new HashMap <Integer, Double>();
  35. // 每月本金
  36. double monthPri = getPerMonthPrincipal(invest, totalMonth);
  37. // 获取月利率
  38. double monthRate = yearRate / 12;
  39. monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
  40. for (int i = 1; i <= totalMonth; i++) {
  41. double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
  42. monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
  43. map.put(i, monthRes);
  44. }
  45. return map;
  46. }
  47. /**
  48. * 等额本金计算获取还款方式为等额本金的每月偿还利息
  49. *
  50. * 公式:每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
  51. *
  52. * @param invest
  53. * 总借款额(贷款本金)
  54. * @param yearRate
  55. * 年利率
  56. * @param month
  57. * 还款总月数
  58. * @return 每月偿还利息
  59. */
  60. public static Map<Integer, Double> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
  61. Map <Integer, Double> inMap = new HashMap <Integer, Double>();
  62. double principal = getPerMonthPrincipal(invest, totalMonth);
  63. Map <Integer, Double> map = getPerMonthPrincipalInterest(invest, yearRate, totalMonth);
  64. for (Map.Entry <Integer, Double> entry : map.entrySet()) {
  65. BigDecimal principalBigDecimal = new BigDecimal(principal);
  66. BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
  67. BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
  68. interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
  69. inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
  70. }
  71. return inMap;
  72. }
  73. /**
  74. * 等额本金计算获取还款方式为等额本金的每月偿还本金
  75. *
  76. * 公式:每月应还本金=贷款本金÷还款月数
  77. *
  78. * @param invest
  79. * 总借款额(贷款本金)
  80. * @param yearRate
  81. * 年利率
  82. * @param month
  83. * 还款总月数
  84. * @return 每月偿还本金
  85. */
  86. public static double getPerMonthPrincipal(double invest, int totalMonth) {
  87. BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
  88. return monthIncome.doubleValue();
  89. }
  90. /**
  91. * 等额本金计算获取还款方式为等额本金的总利息
  92. *
  93. * @param invest
  94. * 总借款额(贷款本金)
  95. * @param yearRate
  96. * 年利率
  97. * @param month
  98. * 还款总月数
  99. * @return 总利息
  100. */
  101. public static double getInterestCount(double invest, double yearRate, int totalMonth) {
  102. BigDecimal count = new BigDecimal(0);
  103. Map <Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);
  104. for (Map.Entry <Integer, Double> entry : mapInterest.entrySet()) {
  105. count = count.add(new BigDecimal(entry.getValue()));
  106. }
  107. return count.doubleValue();
  108. }
  109. /**
  110. * @param args
  111. */
  112. public static void main(String[] args) {
  113. double invest = 10000; // 本金
  114. int month = 12;
  115. double yearRate = 0.15; // 年利率
  116. Map <Integer, Double> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
  117. System.out.println("等额本金---每月本息:" + getPerMonthPrincipalInterest);
  118. double benjin = getPerMonthPrincipal(invest, month);
  119. System.out.println("等额本金---每月本金:" + benjin);
  120. Map <Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, month);
  121. System.out.println("等额本金---每月利息:" + mapInterest);
  122. double count = getInterestCount(invest, yearRate, month);
  123. System.out.println("等额本金---总利息:" + count);
  124. }
  125. }


转自:https://blog.csdn.net/wzygis/article/details/51809575

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值