等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法

墨蓝 2022-04-15 03:06 1038阅读 0赞

等额本金,等额本息,随借随还,利随本清,按月付息到期还本,5种还款方式java计算方法

等额本息定义:本金逐月递增,利息逐月递减,月还款数不变。

等额本金定义:本金保持相同,利息逐月递减,月还款数递减。

随借随还定义:客户在获得授信额度后可以随时来支取,借出后就开始算利息;借出后可以随时还。还了之后在期限内又可以取。

利随本清(一次性还本付息)定义:借款人在贷款期内不是按月偿还本息,而是贷款到期后一次性归还本金和利息。

按月付息到期还本定义:借款人每月只交付利息,在贷款到期日时一次性归还贷款本金

  1. //等额本息
  2. import java.math.BigDecimal;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. /** * 等额本息还款,也称定期付息,即借款人每月按相等的金额偿还贷款本息,其中每月贷款利息按月初剩余贷款本金计算并逐月结清。把按揭贷款的本金总额与利息总额相加, * 然后平均分摊到还款期限的每个月中。作为还款人,每个月还给银行固定金额,但每月还款额中的本金比重逐月递增、利息比重逐月递减。 */
  6. public class AverageCapitalPlusInterestUtils {
  7. /** * 等额本息计算获取还款方式为等额本息的每月偿还本金和利息 * * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalmonth * 还款总月数 * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位 */
  8. public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalmonth) {
  9. double monthRate = yearRate / 12;
  10. BigDecimal monthIncome = new BigDecimal(invest)
  11. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  12. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  13. return monthIncome.doubleValue();
  14. }
  15. /** * 等额本息计算获取还款方式为等额本息的每月偿还利息 * * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕 * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalmonth * 还款总月数 * @return 每月偿还利息 */
  16. public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalmonth) {
  17. Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
  18. double monthRate = yearRate/12;
  19. BigDecimal monthInterest;
  20. for (int i = 1; i < totalmonth + 1; i++) {
  21. BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
  22. BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalmonth)).subtract(new BigDecimal(Math.pow(1 + monthRate, i-1)));
  23. monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 6, BigDecimal.ROUND_DOWN);
  24. monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
  25. map.put(i, monthInterest);
  26. }
  27. return map;
  28. }
  29. /** * 等额本息计算获取还款方式为等额本息的每月偿还本金 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalmonth * 还款总月数 * @return 每月偿还本金 */
  30. public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalmonth) {
  31. double monthRate = yearRate / 12;
  32. BigDecimal monthIncome = new BigDecimal(invest)
  33. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  34. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  35. Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
  36. Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();
  37. for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
  38. mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
  39. }
  40. return mapPrincipal;
  41. }
  42. /** * 等额本息计算获取还款方式为等额本息的总利息 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalmonth * 还款总月数 * @return 总利息 */
  43. public static double getInterestCount(double invest, double yearRate, int totalmonth) {
  44. BigDecimal count = new BigDecimal(0);
  45. Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalmonth);
  46. for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
  47. count = count.add(entry.getValue());
  48. }
  49. return count.doubleValue();
  50. }
  51. /** * 应还本金总和 * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalmonth * 还款总月数 * @return 应还本金总和 */
  52. public static double getPrincipalInterestCount(double invest, double yearRate, int totalmonth) {
  53. double monthRate = yearRate / 12;
  54. BigDecimal perMonthInterest = new BigDecimal(invest)
  55. .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalmonth)))
  56. .divide(new BigDecimal(Math.pow(1 + monthRate, totalmonth) - 1), 2, BigDecimal.ROUND_DOWN);
  57. BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalmonth));
  58. count = count.setScale(2, BigDecimal.ROUND_DOWN);
  59. return count.doubleValue();
  60. }
  61. //等额本金
  62. import java.math.BigDecimal;
  63. import java.util.HashMap;
  64. import java.util.Map;
  65. /** * 等额本金是指一种贷款的还款方式,是在还款期内把贷款数总额等分,每月偿还同等数额的本金和剩余贷款在该月所产生的利息,这样由于每月的还款本金额固定, * 而利息越来越少,借款人起初还款压力较大,但是随时间的推移每月还款数也越来越少。 */
  66. public class AverageCapitalUtils {
  67. /** * 等额本金计算获取还款方式为等额本金的每月偿还本金和利息 * * 公式:每月偿还本金=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalMonth * 还款总月数 * @return 每月偿还本金和利息,不四舍五入,直接截取小数点最后两位 */
  68. public static Map<Integer, Double> getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
  69. Map<Integer, Double> map = new HashMap<Integer, Double>();
  70. // 每月本金
  71. double monthPri = getPerMonthPrincipal(invest, totalMonth);
  72. // 获取月利率
  73. double monthRate = yearRate / 12;
  74. monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
  75. for (int i = 1; i <= totalMonth; i++) {
  76. double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
  77. monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
  78. map.put(i, monthRes);
  79. }
  80. return map;
  81. }
  82. /** * 等额本金计算获取还款方式为等额本金的每月偿还利息 * * 公式:每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalMonth * 还款总月数 * @return 每月偿还利息 */
  83. public static Map<Integer, Double> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
  84. Map<Integer, Double> inMap = new HashMap<Integer, Double>();
  85. double principal = getPerMonthPrincipal(invest, totalMonth);
  86. Map<Integer, Double> map = getPerMonthPrincipalInterest(invest, yearRate, totalMonth);
  87. for (Map.Entry<Integer, Double> entry : map.entrySet()) {
  88. BigDecimal principalBigDecimal = new BigDecimal(principal);
  89. BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
  90. BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
  91. interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
  92. inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
  93. }
  94. return inMap;
  95. }
  96. /** * 等额本金计算获取还款方式为等额本金的每月偿还本金 * * 公式:每月应还本金=贷款本金÷还款月数 * * @param invest * 总借款额(贷款本金) * @param totalMonth * 还款总月数 * @return 每月偿还本金 */
  97. public static double getPerMonthPrincipal(double invest, int totalMonth) {
  98. BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
  99. return monthIncome.doubleValue();
  100. }
  101. /** * 等额本金计算获取还款方式为等额本金的总利息 * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalMonth * 还款总月数 * @return 总利息 */
  102. public static double getInterestCount(double invest, double yearRate, int totalMonth) {
  103. BigDecimal count = new BigDecimal(0);
  104. Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);
  105. for (Map.Entry<Integer, Double> entry : mapInterest.entrySet()) {
  106. count = count.add(new BigDecimal(entry.getValue()));
  107. }
  108. return count.doubleValue();
  109. }
  110. //随借随还
  111. /** 随借随还 * @param amount 本金 * @param annualInterestRate 年利率 * @param termCount 还款期限(天数) * @return 还款金额 = 本金 + 本金*日利率*天数 * @throws Exception */
  112. public static BigDecimal payOff(BigDecimal amount, BigDecimal annualInterestRate, int termCount) throws Exception {
  113. annualInterestRate = annualInterestRate.multiply(new BigDecimal(100));
  114. BigDecimal dailyInterest = LoanUtil.getDayInterestByYear(annualInterestRate);
  115. BigDecimal multiply = amount.multiply(dailyInterest).multiply(new BigDecimal(termCount));
  116. return multiply.add(amount).setScale(2, BigDecimal.ROUND_HALF_EVEN);
  117. }
  118. //利随本清
  119. /** * @Title: calculateInterest 利息总计 = 本金*利率*数 * @Description: TODO(利随本清利息计算) * @param @param amount 本金 * @param @param annualInterestRate 年利率 * @param @param termUnit 1为日利率 3为月利率 * @param @param termCount 日数,月数 * @param @return 利随本清方式利息总计 * @param @throws Exception参数 * @return BigDecimal 返回类型 * @throws * */
  120. public static BigDecimal calculateInterest(BigDecimal amount,
  121. BigDecimal annualInterestRate, int termUnit, int termCount) throws Exception
  122. {
  123. annualInterestRate = annualInterestRate.multiply(new BigDecimal(100));
  124. if (termUnit == 1) {
  125. BigDecimal dailyInterest = LoanUtil
  126. .getDayInterestByYear(annualInterestRate);
  127. return amount.multiply(dailyInterest)
  128. .multiply(new BigDecimal(termCount))
  129. .setScale(2,
  130. BigDecimal.ROUND_HALF_EVEN);
  131. } else if (termUnit == 3) {
  132. BigDecimal monthlyInterest = LoanUtil
  133. .getMonthInterestByYear(annualInterestRate);
  134. return amount.multiply(monthlyInterest)
  135. .multiply(new BigDecimal(termCount))
  136. .setScale(2, BigDecimal.ROUND_HALF_EVEN);
  137. } else {
  138. throw new Exception("不支持的时间单位: " + termUnit);
  139. }
  140. }
  141. //按月付息到期还本
  142. /** * 按月付息,到期还本-计算获取还款方式为按月付息,到期还本的每月偿还利息 * * 公式:每月应还利息=总借款额*年利率÷还款月数 * * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalMonth * 还款总月数 * @return 每月偿还利息 */
  143. public static double getPerMonthInterests(double invest,double yearRate, int totalMonth) {
  144. BigDecimal monthIncome = new BigDecimal(invest).multiply(new BigDecimal(yearRate)).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
  145. return monthIncome.doubleValue();
  146. }
  147. /** * 按月付息,到期还本计算获取还款方式为按月付息,到期还本的每月偿还利息,最后一个月归还利息+本金 * * 公式:每月偿还利息=(总借款额*年利率÷还款月数) * * @param invest * 总借款额(贷款本金) * @param yearRate * 年利率 * @param totalMonth * 还款总月数 * @return 每月偿还利息,不四舍五入,直接截取小数点最后两位 */
  148. public static Map<Integer, Double> getPerMonthPrincipalInterests(double invest, double yearRate, int totalMonth) {
  149. Map<Integer, Double> map = new HashMap<Integer, Double>();
  150. // 每月利息
  151. double monthPri = getPerMonthInterests(invest,yearRate, totalMonth);
  152. for (int i = 1; i <= totalMonth; i++) {
  153. if(i == totalMonth){
  154. //最后一期 利息+ 本金
  155. monthPri = monthPri + invest ;
  156. }
  157. map.put(i, monthPri);
  158. }
  159. return map;
  160. }

发表评论

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

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

相关阅读