Bank equal principal and interest repayment algorithm

Equal principal and interest algorithm formula

Monthly repayment amount = [loan principal × monthly interest rate × (1 + monthly interest rate) ^ repayment months] ÷ [(1 + monthly interest rate) ^ repayment months - 1]

/**
 * Description: Equal principal and interest repayment algorithm
 *
 * @author ljf <[email protected]>
 * @date 2017-2-6 4:28:55 pm
 * @version V1.2
 */
public class PrincipalAndInterestEquals {

   /**
    * @param args
    */
   public static void main(String[] args) {
       BigDecimal invest = new BigDecimal(139000); // 本金
       double yearRate = 0.059; // annual interest rate
       int year = 15;//Duration
       double monthRate = yearRate / 12;
       int month = year * 12;
       // Monthly principal and interest amount = (principal × monthly interest rate × (1 + monthly interest rate) ^ repayment months) ÷ ((1 + monthly interest rate) ^ repayment months - 1)
       BigDecimal monthIncome = invest.multiply(new BigDecimal(monthRate * Math.pow(1 +
               monthRate, month))).divide(new BigDecimal(Math.pow(1 + monthRate, month) - 1), 2,
               BigDecimal.ROUND_HALF_UP);
       System.out.println("Monthly principal and interest amount: " + monthIncome);
       System.out.println("---------------------------------------------------");
       // Monthly principal = principal × monthly interest rate × (1+monthly interest rate)^(repayment month serial number-1)÷((1+monthly interest rate)^repayment months-1)
       BigDecimal monthCapital;
       BigDecimal sumCapital = BigDecimal.ZERO;
       for (int i = 1; i < month + 1; i++) {
           monthCapital = invest.multiply(new BigDecimal(monthRate * (Math.pow((1 + monthRate),
                   i - 1)))).divide(new BigDecimal(Math.pow(1 + monthRate, month) - 1), 2,
                   BigDecimal.ROUND_HALF_UP);
           System.out.println("Section" + i + "Month Capital: " + monthCapital);
           sumCapital = sumCapital.add(monthCapital);
       }
       System.out.println("---------------------------------------------------");
       // Monthly Interest = Remaining Principal x Loan Monthly Interest Rate
       BigDecimal monthInterest;
       BigDecimal capital = invest;
       BigDecimal tmpCapital = BigDecimal.ZERO;
       BigDecimal sumInterest = BigDecimal.ZERO;
       for (int i = 1; i < month + 1; i++) {
           capital = capital.subtract(tmpCapital);
           monthInterest = capital.multiply(new BigDecimal(monthRate)).setScale(2, BigDecimal
                   .ROUND_HALF_UP);
           tmpCapital = invest.multiply(new BigDecimal(monthRate * (Math.pow((1 + monthRate), i
                   - 1)))).divide(new BigDecimal(Math.pow(1 + monthRate, month) - 1), 2,
                   BigDecimal.ROUND_HALF_UP);
           System.out.println("第" + i + "月利息: " + monthInterest);

           sumInterest = sumInterest.add (monthInterest);
       }
       System.out.println("Principal sum: " + sumCapital + " Interest sum: " + sumInterest);
   }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313174&siteId=291194637