java等额本息、等额本金计算(用了好久才知道等额本息原来这样算,记录一下)

 
  
public static void main(String[] args) {
         double invest = 280000;
         double yearRate = 0.0655;
         double monthRate = yearRate/12;
         int year = 15;
         int month = year * 12;
         // 每月本息金额  = (本金×月利率×(1+月利率)^还款月数)÷ ((1+月利率)^还款月数-1)
         double monthIncome = (invest* monthRate * Math.pow(1+monthRate,month))
/(Math.pow(1+monthRate,month)-1);
         System.out.println("每月本息金额 : "+ monthIncome);
        
// 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1)
         double monthCapital = 0;
         for(inti=1;i1;i++){
                 monthCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))
/(Math.pow(1+monthRate,month)-1);
                 System.out.println("第"+ i + "月本金: "+ monthCapital);
         }
                
// 每月利息  = 剩余本金 x 贷款月利率
         double monthInterest = 0;
         double capital = invest; double sumAvg = 0;
         double tmpCapital = 0;
             for(inti=1;i1;i++){
                     capital = capital - tmpCapital;
                     monthInterest = capital * monthRate;
                     tmpCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))
/(Math.pow(1+monthRate,month)-1);
                     System.out.println("第"+ i + "月利息: "+ monthInterest);                  
                    sumAvg += monthInterest
             }
System.out.println
("总利息: "+ sumAvg); DecimalFormat decimalFormat = new DecimalFormat("0.00");
System.out.println
("总利息输出两位小数: "+ Double.parseDouble(decimalFormat.format(sumAvg)));
        
}

你可能感兴趣的:(java等额本息、等额本金计算(用了好久才知道等额本息原来这样算,记录一下))