Java实现等额本金

心已赠人 2021-11-17 23:24 351阅读 0赞
  1. import java.math.BigDecimal;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. /**
  7. * 等额本金
  8. */
  9. public class EqualPrincipalUtil {
  10. /**
  11. * 计算获取还款方式为等额本金的每期偿还本金和利息
  12. *
  13. * 公式:每期偿还本金=(贷款本金÷还款期数)+(贷款本金-已归还本金累计额)×期利率
  14. *
  15. * @param invest
  16. * 总借款额(贷款本金)
  17. * @param periodRate
  18. * 年利率
  19. * @param periodNum
  20. * 还款总期数
  21. * @return 每期偿还本金和利息,四舍五入
  22. */
  23. public static Map<Integer, Double> getPerPeriodPrincipalInterest(double invest, double periodRate, int periodNum) {
  24. Map<Integer, Double> map = new HashMap<Integer, Double>();
  25. // 每期本金
  26. double period = getPerPeriodPrincipal(invest, periodNum);
  27. // 获取期利率
  28. periodRate = new BigDecimal(periodRate).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
  29. for (int i = 1; i <= periodNum; i++) {
  30. double monthRes = period + (invest - period * (i - 1)) * periodRate;
  31. monthRes = new BigDecimal(monthRes).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
  32. map.put(i, monthRes);
  33. }
  34. return map;
  35. }
  36. /**
  37. * 等额本金计算获取还款方式为等额本金的每期偿还利息
  38. *
  39. * 公式:每期应还利息=剩余本金×期利率=(贷款本金-已归还本金累计额)×期利率
  40. *
  41. * @param invest
  42. * 总借款额(贷款本金)
  43. * @param periodRate
  44. * 年利率
  45. * @param periodNum
  46. * 还款总期数
  47. * @return 每期偿还利息
  48. */
  49. public static Map<Integer, BigDecimal> getPerPeriodInterest(double invest, double periodRate, int periodNum) {
  50. Map<Integer, BigDecimal> inMap = new HashMap<Integer, BigDecimal>();
  51. double principal = getPerPeriodPrincipal(invest, periodNum);
  52. Map<Integer, Double> map = getPerPeriodPrincipalInterest(invest, periodRate, periodNum);
  53. for (Map.Entry<Integer, Double> entry : map.entrySet()) {
  54. BigDecimal principalBigDecimal = new BigDecimal(principal);
  55. BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
  56. BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
  57. interestBigDecimal = interestBigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP);
  58. inMap.put(entry.getKey(), interestBigDecimal);
  59. }
  60. return inMap;
  61. }
  62. /**
  63. * 等额本金计算获取还款方式为等额本金的每期偿还本金
  64. *
  65. * 公式:每期应还本金=贷款本金÷还款期数
  66. *
  67. * @param invest
  68. * 总借款额(贷款本金)
  69. * @param periodNum
  70. * 还款总期数
  71. * @return 每期偿还本金
  72. */
  73. public static double getPerPeriodPrincipal(double invest, int periodNum) {
  74. BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(periodNum), 4, BigDecimal.ROUND_HALF_UP);
  75. return monthIncome.doubleValue();
  76. }
  77. /**
  78. * 等额本金计算获取还款方式为等额本金的总利息
  79. *
  80. * @param invest
  81. * 总借款额(贷款本金)
  82. * @param periodRate
  83. * 年利率
  84. * @param periodNum
  85. * 还款总期数
  86. * @return 总利息
  87. */
  88. public static double getInterestCount(double invest, double periodRate, int periodNum) {
  89. BigDecimal count = new BigDecimal(0);
  90. Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
  91. for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
  92. count = count.add(entry.getValue());
  93. }
  94. return count.doubleValue();
  95. }
  96. /**
  97. * 本息总和
  98. * @param invest
  99. * 本金
  100. * @param periodRate
  101. * 期利率
  102. * @param periodNum
  103. * 期数
  104. * @return
  105. */
  106. public static double getPrincipalInterestCount(double invest, double periodRate, int periodNum) {
  107. BigDecimal perMonthInterest = new BigDecimal(invest);
  108. Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
  109. for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
  110. perMonthInterest = perMonthInterest.add(entry.getValue());
  111. }
  112. return perMonthInterest.doubleValue();
  113. }
  114. /**
  115. * 每期还款日期
  116. * @param start_date
  117. * 起租日
  118. * @param perPeriodMonthNum
  119. * 每期月数
  120. * @param periodNum
  121. * 期数
  122. * @return
  123. */
  124. public static Map<Integer, String> getRepaymentDate(String start_date, int perPeriodMonthNum, int periodNum) {
  125. Map<Integer, String> periodRepaymentDate = new HashMap<Integer, String>();
  126. String nextRepaymentDate = start_date;
  127. periodRepaymentDate.put(1, nextRepaymentDate);
  128. for (int i = 2; i < periodNum + 1; i++) {
  129. nextRepaymentDate = getMonthAdd(perPeriodMonthNum, nextRepaymentDate, "yyyyMMdd");
  130. periodRepaymentDate.put(i, nextRepaymentDate);
  131. }
  132. return periodRepaymentDate;
  133. }
  134. /**
  135. * 功能描述:返回指定日期加上多少月之后的时间<BR>
  136. * @param from yyyyMMdd
  137. * @param day
  138. * @param formatStr
  139. * @return
  140. */
  141. public static String getMonthAdd(int day,String from,String formatStr){
  142. SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
  143. Calendar calendar = Calendar.getInstance();
  144. try {
  145. calendar.setTime(sdf.parse(from));
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. calendar.add(Calendar.MONTH, day);
  150. String date = sdf.format(calendar.getTime());
  151. return date;
  152. }
  153. public static void main(String[] args) {
  154. double invest = 10000; // 本金
  155. int periodNum = 4;
  156. double periodRate = 0.12/3; // 年利率
  157. Map<Integer, Double> getPerMonthPrincipalInterest = getPerPeriodPrincipalInterest(invest, periodRate, periodNum);
  158. System.out.println("等额本金---每期本息:" + getPerMonthPrincipalInterest);
  159. double benjin = getPerPeriodPrincipal(invest, periodNum);
  160. System.out.println("等额本金---每期本金:" + benjin);
  161. Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
  162. System.out.println("等额本金---每期利息:" + mapInterest);
  163. double count = getInterestCount(invest, periodRate, periodNum);
  164. System.out.println("等额本金---总利息:" + count);
  165. double principalInterestCount = getPrincipalInterestCount(invest,periodRate,periodNum);
  166. System.out.println("等额本金---本息:" + principalInterestCount);
  167. }
  168. }

发表评论

表情:
评论列表 (有 0 条评论,351人围观)

还没有评论,来说两句吧...

相关阅读