使用java房贷月供计算

月供计算简介

使用等额本息方式计算月供信息,包含每月月供,月供中利息和本金信息,总利息,总利息占本金比例等

  • v1.0版本: 最基础计算月供代码
  • v2.0版本: 支持自定义查看最近n个月的月还款计划。并显示还款本金及利息
  • v3.0版本: 还款计算补充剩余本金,支持计算总利息及总利息占比

最新版效果:

贷款金额: 1000000.00元 月供(5.00%利率): 5368.22 元
最近3个月还款详情:
第1月:还款本金 1201.55 元,利息 4166.67 元,剩余本金 998798.45 元
第2月:还款本金 1206.56 元,利息 4161.66 元,剩余本金 997591.89 元
第3月:还款本金 1211.58 元,利息 4156.63 元,剩余本金 996380.31 元
利息总额:932557.84 元
利息占比:93.26%
==============================================
贷款金额: 545143.55元 月供(4.50%利率): 2909.54 元
最近3个月还款详情:
第1月:还款本金 865.25 元,利息 2044.29 元,剩余本金 544278.30 元
第2月:还款本金 868.50 元,利息 2041.04 元,剩余本金 543409.80 元
第3月:还款本金 871.76 元,利息 2037.79 元,剩余本金 542538.04 元
利息总额:397548.32 元
利息占比:72.93%
==============================================
>>>>【提前还贷20万】<<<<
贷款金额: 345143.55元 月供(4.50%利率): 1842.10 元
最近3个月还款详情:
第1月:还款本金 547.81 元,利息 1294.29 元,剩余本金 344595.74 元
第2月:还款本金 549.87 元,利息 1292.23 元,剩余本金 344045.87 元
第3月:还款本金 551.93 元,利息 1290.17 元,剩余本金 343493.94 元
利息总额:251697.45 元
利息占比:72.93%
==============================================

月供公式

下面是使用Java代码实现商业贷款月供计算的例子。代码中使用了公式:
在这里插入图片描述

月供计算代码v1.0

public class LoanCalculator {

    public static void main(String[] args) {
        // 贷款本金、贷款期限、年利率
        calculateAndPrintEMI(545143.55, 27, 4.6);
        calculateAndPrintEMI(545143.55, 27, 4.5);
        System.out.println("=====提前还贷20万=====");
        calculateAndPrintEMI(545143.55-200000, 27, 4.5);
    }

    /**
     *
     * @param loanAmount 贷款本金
     * @param loanTermYears 贷款期限(年)
     * @param interestRate 年利率(%)
     */
    private static void calculateAndPrintEMI(double loanAmount, int loanTermYears, double interestRate) {
        // 计算月利率
        double monthlyInterestRate = interestRate / (12 * 100);
        // 计算还款月数
        int numPayments = loanTermYears * 12;
        // 计算月供
        double emi = calculateEMI(loanAmount, monthlyInterestRate, numPayments);
        // 打印结果
        System.out.printf("月供(%.2f%%利率): %.2f 元\n", interestRate, emi);
    }

    private static double calculateEMI(double loanAmount, double monthlyInterestRate, int numPayments) {
        return loanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numPayments)) /
                (Math.pow(1 + monthlyInterestRate, numPayments) - 1);
    }
}

执行结果

月供(4.60%利率): 2941.16 元
月供(4.50%利率): 2909.54 元
=====提前还贷20万=====
月供(4.50%利率): 1842.10 元

月供计算代码v2.0

在1.0版本基础上,支持自定义查看最近n个月的月还款计划。并显示还款本金及利息

public class LoanCalculator {

    // 自定义需要查看的月数
    private static final int MONTHS_TO_VIEW = 5;

    public static void main(String[] args) {
        // 贷款本金、贷款期限、年利率
        calculateAndPrintEMI(545143.55, 27, 4.6);
        calculateAndPrintEMI(545143.55, 27, 4.5);
        System.out.println("=====提前还贷20万=====");
        calculateAndPrintEMI(545143.55 - 200000, 27, 4.5);
    }

    /**
     * @param loanAmount      贷款本金
     * @param loanTermYears   贷款期限(年)
     * @param interestRate    年利率(%)
     */
    private static void calculateAndPrintEMI(double loanAmount, int loanTermYears, double interestRate) {
        // 计算月利率
        double monthlyInterestRate = interestRate / (12 * 100);
        // 计算还款月数
        int numPayments = loanTermYears * 12;
        // 计算月供
        double emi = calculateEMI(loanAmount, monthlyInterestRate, numPayments);
        // 打印结果
        System.out.printf("%.2f 月供(%.2f%%利率): %.2f 元\n", loanAmount, interestRate, emi);

        // 计算并打印指定月数的还款本金和利息
        printLastMonthsDetails(loanAmount, monthlyInterestRate, numPayments, emi, MONTHS_TO_VIEW);
    }

    private static double calculateEMI(double loanAmount, double monthlyInterestRate, int numPayments) {
        return loanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numPayments)) /
                (Math.pow(1 + monthlyInterestRate, numPayments) - 1);
    }

    private static void printLastMonthsDetails(double loanAmount, double monthlyInterestRate, int numPayments, double emi, int monthsToView) {
        // 初始化存储每月还款详情的列表
        List<MonthlyPaymentDetails> paymentDetailsList = new ArrayList<>();

        // 计算并保存每个月的还款本金和利息
        double remainingLoanAmount = loanAmount;
        for (int month = 1; month <= monthsToView; month++) {
            double interestPaid = remainingLoanAmount * monthlyInterestRate;
            double principalPaid = emi - interestPaid;
            remainingLoanAmount -= principalPaid;

            // 将每月的还款详情保存到列表中
            paymentDetailsList.add(new MonthlyPaymentDetails(month, principalPaid, interestPaid));
        }

        // 打印指定月数的还款详情
        System.out.printf("最近%d个月还款详情:\n", monthsToView);
        for (MonthlyPaymentDetails details : paymentDetailsList) {
            System.out.printf("第%d月:还款本金 %.2f 元,利息 %.2f 元\n",
                    details.getMonth(), details.getPrincipalPaid(), details.getInterestPaid());
        }
    }

    private static class MonthlyPaymentDetails {
        private final int month;
        private final double principalPaid;
        private final double interestPaid;

        public MonthlyPaymentDetails(int month, double principalPaid, double interestPaid) {
            this.month = month;
            this.principalPaid = principalPaid;
            this.interestPaid = interestPaid;
        }

        public int getMonth() {
            return month;
        }

        public double getPrincipalPaid() {
            return principalPaid;
        }

        public double getInterestPaid() {
            return interestPaid;
        }
    }
}

执行结果

545143.55W 月供(4.60%利率): 2941.16 元
最近5个月还款详情:
第1月:还款本金 851.44 元,利息 2089.72 元
第2月:还款本金 854.71 元,利息 2086.45 元
第3月:还款本金 857.98 元,利息 2083.18 元
第4月:还款本金 861.27 元,利息 2079.89 元
第5月:还款本金 864.58 元,利息 2076.59 元
545143.55W 月供(4.50%利率): 2909.54 元
最近5个月还款详情:
第1月:还款本金 865.25 元,利息 2044.29 元
第2月:还款本金 868.50 元,利息 2041.04 元
第3月:还款本金 871.76 元,利息 2037.79 元
第4月:还款本金 875.03 元,利息 2034.52 元
第5月:还款本金 878.31 元,利息 2031.24 元
=====提前还贷20万=====
345143.55W 月供(4.50%利率): 1842.10 元
最近5个月还款详情:
第1月:还款本金 547.81 元,利息 1294.29 元
第2月:还款本金 549.87 元,利息 1292.23 元
第3月:还款本金 551.93 元,利息 1290.17 元
第4月:还款本金 554.00 元,利息 1288.10 元
第5月:还款本金 556.08 元,利息 1286.02 元

月供计算代码v3.0

在还款计算补充剩余本金,支持计算总利息及总利息占比

public class LoanCalculatorV3 {

    //需要查看最近n个月还款详情
    private static final int MONTHS_TO_VIEW = 3;

    public static void main(String[] args) {
        calculateAndPrintEMI(1000000, 30, 5.0);
        calculateAndPrintEMI(545143.55, 27, 4.5);
        System.out.println("=====提前还贷20万=====");
        calculateAndPrintEMI(545143.55 - 200000, 27, 4.5);
    }

    private static void calculateAndPrintEMI(double loanAmount, int loanTermYears, double interestRate) {

        double monthlyInterestRate = interestRate / (12 * 100);
        int numPayments = loanTermYears * 12;
        double emi = calculateEMI(loanAmount, monthlyInterestRate, numPayments);
        System.out.printf("贷款金额: %.2f元 月供(%.2f%%利率): %.2f 元\n", loanAmount, interestRate, emi);

        // 计算并打印指定月数的还款本金、利息和剩余本金
        List<MonthlyPaymentDetails> paymentDetailsList = printLastMonthsDetails(loanAmount, monthlyInterestRate, numPayments, emi, MONTHS_TO_VIEW);

        // 计算总利息
        double totalInterest = calculateTotalInterest(emi, numPayments, loanAmount);
        System.out.printf("利息总额:%.2f 元\n", totalInterest);

        // 计算总利息占贷款本金的比例
        double interestToPrincipalRatio = totalInterest / loanAmount;
        System.out.printf("利息占比:%.2f%%\n", interestToPrincipalRatio * 100);
    }

    private static double calculateEMI(double loanAmount, double monthlyInterestRate, int numPayments) {
        return loanAmount * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numPayments) /
                (Math.pow(1 + monthlyInterestRate, numPayments) - 1);
    }

    private static List<MonthlyPaymentDetails> printLastMonthsDetails(double loanAmount, double monthlyInterestRate, int numPayments, double emi, int monthsToView) {
        List<MonthlyPaymentDetails> paymentDetailsList = new ArrayList<>();
        double remainingLoanAmount = loanAmount;

        for (int month = 1; month <= monthsToView; month++) {
            double interestPaid = remainingLoanAmount * monthlyInterestRate;
            double principalPaid = emi - interestPaid;
            remainingLoanAmount -= principalPaid;

            paymentDetailsList.add(new MonthlyPaymentDetails(month, principalPaid, interestPaid, remainingLoanAmount));
        }

        System.out.printf("最近%d个月还款详情:\n", monthsToView);
        for (MonthlyPaymentDetails details : paymentDetailsList) {
            System.out.printf("第%d月:还款本金 %.2f 元,利息 %.2f 元,剩余本金 %.2f 元\n",
                    details.getMonth(), details.getPrincipalPaid(), details.getInterestPaid(), details.getRemainingLoanAmount());
        }

        return paymentDetailsList;
    }

    private static double calculateTotalInterest(double emi, int numPayments, double loanAmount) {
        return emi * numPayments - loanAmount;
    }

    private static class MonthlyPaymentDetails {
        private final int month;
        private final double principalPaid;
        private final double interestPaid;
        private final double remainingLoanAmount;

        public MonthlyPaymentDetails(int month, double principalPaid, double interestPaid, double remainingLoanAmount) {
            this.month = month;
            this.principalPaid = principalPaid;
            this.interestPaid = interestPaid;
            this.remainingLoanAmount = remainingLoanAmount;
        }

        public int getMonth() {
            return month;
        }

        public double getPrincipalPaid() {
            return principalPaid;
        }

        public double getInterestPaid() {
            return interestPaid;
        }

        public double getRemainingLoanAmount() {
            return remainingLoanAmount;
        }
    }
}

执行结果

贷款金额: 1000000.00元 月供(5.00%利率): 5368.22 元
最近3个月还款详情:
第1月:还款本金 1201.55 元,利息 4166.67 元,剩余本金 998798.45 元
第2月:还款本金 1206.56 元,利息 4161.66 元,剩余本金 997591.89 元
第3月:还款本金 1211.58 元,利息 4156.63 元,剩余本金 996380.31 元
利息总额:932557.84 元
利息占比:93.26%
==============================================
贷款金额: 545143.55元 月供(4.50%利率): 2909.54 元
最近3个月还款详情:
第1月:还款本金 865.25 元,利息 2044.29 元,剩余本金 544278.30 元
第2月:还款本金 868.50 元,利息 2041.04 元,剩余本金 543409.80 元
第3月:还款本金 871.76 元,利息 2037.79 元,剩余本金 542538.04 元
利息总额:397548.32 元
利息占比:72.93%
==============================================
>>>>【提前还贷20万】<<<<
贷款金额: 345143.55元 月供(4.50%利率): 1842.10 元
最近3个月还款详情:
第1月:还款本金 547.81 元,利息 1294.29 元,剩余本金 344595.74 元
第2月:还款本金 549.87 元,利息 1292.23 元,剩余本金 344045.87 元
第3月:还款本金 551.93 元,利息 1290.17 元,剩余本金 343493.94 元
利息总额:251697.45 元
利息占比:72.93%
==============================================
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值