java等额本金和等额本息计算方式

作者在 2023-09-20 15:48:16 发布以下内容
直接贴代码
package com.cn.fajia;

/***
 * <p>
 * 房价和还款利息计算
 *
 */
public class Fajita {
    public static void main(String[] args) {

        //房子总价
        double totalPrice = 1428571;
        //首付比例
        double firstPaymentRatio = 0.6;
        //贷款总年数
        int yearNum = 30;
        //贷款总金额
        double allLoan = totalPrice * (1 - firstPaymentRatio);
        //月利率
        double ratio = 0.004167;
        double principal = getPrincipal(yearNum,allLoan);
        //等额本金
        double averageCapitalPayAmount = getAverageCapitalPayAmount(yearNum,allLoan,ratio);
        //等额本金月还款利息
        double averageCapitalPayAmountInterest = getAverageCapitalPayAmountInterest(principal,averageCapitalPayAmount);
        double allPrincipalInterest = 0.0,allAverageCapitalPayAmountInterest = 0.0;
        for(int month=1;month<=yearNum*12;month++){

            //这里有金额损失,可以前面所有期都使用去尾法保留指定位数的小数点,然后最后一期按照总贷款金额减去前面所有贷款本金,计算出最后一期应还贷款本金
            //利息的小数点统一按照四舍五入的方式保留小数
            double principalInterest = getPrincipalInterest(yearNum,allLoan,month,ratio);
//            if(month == yearNum * 12){
//                principalInterest =
//            }else{
//                principalInterest = ;
//            }


            allPrincipalInterest += principalInterest;
            allAverageCapitalPayAmountInterest += averageCapitalPayAmountInterest;

            System.out.println("第"+month+"个月等额本息还款本金:"+principal+";利息:"+principalInterest+",当月总还款额:"+(principal+principalInterest));
            System.out.println("第"+month+"个月等额本金还款本金:"+principal+";利息:"+averageCapitalPayAmountInterest+",当月总还款额:"+averageCapitalPayAmount);
            System.out.println(" ");
        }

        System.out.println("贷款总额:"+allLoan+";贷款时间:"+yearNum+"年;等额本息总利息:"+allPrincipalInterest+";等额本金总利息:"+allAverageCapitalPayAmountInterest);
    }

    /**
     *
     *当月还款本金
     *
     **/
    public static double getPrincipal(int yearNum,double allLoan){

        //月均本金

        return allLoan/(yearNum*12);
    }

    /**
     *
     *  等额本金 还款月当月利息
     *  repaymentMonth          还款月
     *  每月还款金额=(贷款本金/还款月数)+(本金—已归还本金累计额)×每月利率
     ***/
    public static double getPrincipalInterest(int yearNum,double allLoan,int repaymentMonth,double ratio){

        //计算剩余还款月
        int remainingRepaymentMonth = yearNum * 12 - repaymentMonth;
        //计算剩余贷款本金
        double remainingLoan = getPrincipal(yearNum,allLoan) * remainingRepaymentMonth;

        return remainingLoan * ratio;
    }

    /**
     *
     * 等额本息 每月还款额
     * 等额本息每月还款额 = [贷款本金 × 月利率 × (1 + 月利率)^贷款期数] / [(1 + 月利率)^贷款期数 - 1]
     *
     */

    public static double getAverageCapitalPayAmount(int yearNum,double allLoan,double ratio){

        //贷款月
        int payMonth = yearNum * 12;

        //计算临时利率
        double tmpRatio = Math.pow(1+ratio,payMonth);
//        double tmpRatio = 0.0;
//        for(int i = 1;i<=payMonth;i++){
//            tmpRatio = tmpRatio == 0.0?(1 + ratio):tmpRatio * (1 + ratio);
//        }
        return allLoan * ratio * tmpRatio / (tmpRatio - 1);
    }

    /***
     *
     * @param principal     等额本金月还款本金
     * @param averageCapitalPayAmount   等额本金月还款金额
     * @return
     */
    public static double getAverageCapitalPayAmountInterest(double principal,double averageCapitalPayAmount){

        return averageCapitalPayAmount - principal;
    }


}

java | 阅读 1871 次
文章评论,共0条
游客请输入验证码