Java实现等额本息

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

发表评论

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

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

相关阅读