You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript贷款利息计算器复利计算错误修正咨询

问题分析与解决方案

你的代码当前使用单利计算逻辑,这就是导致结果偏差的原因。要实现正确的复利(等额本息)贷款计算,需要替换核心的计算公式,以下是具体修改步骤:

核心修改点

  • 转换利率和期限为月度单位:复利贷款计算基于月利率和总还款月数,而非年利率和年数
  • 使用等额本息公式计算月供:替换原单利均分的计算方式
  • 重新计算总还款额和总利息:基于月供和总月数推导,而非单利利息加本金

修改后的核心计算代码

function calculateResults(e) {
    // ui elements
    const principal = document.querySelector('#principal');
    const rate = document.querySelector('#rate');
    const time = document.querySelector('#time');
    const monthlyPayment = document.querySelector('#payment');
    const totalInterest = document.querySelector('#interest');
    const totalAmount = document.querySelector('#total');
    // formula variables
    const p = parseFloat(principal.value);
    const annualRate = parseFloat(rate.value);
    const years = parseFloat(time.value);
    
    // 转换为月度计算单位
    const monthlyRate = annualRate / 12 / 100;
    const totalMonths = years * 12;

    // 等额本息月供公式
    const payment = (p * monthlyRate * Math.pow(1 + monthlyRate, totalMonths)) / 
                    (Math.pow(1 + monthlyRate, totalMonths) - 1);

    // 计算总还款额和总利息
    const total = payment * totalMonths;
    const interest = total - p;
    
    if (isFinite(payment)) {
        totalInterest.innerHTML = interest.toFixed(2) + 'บาท';
        monthlyPayment.innerHTML = payment.toFixed(2) + 'บาท';
        totalAmount.innerHTML = total.toFixed(2)+ 'บาท';
        // hide loader
        button.classList.remove('loading');
        // show results
        results.classList.remove('hide');
    } else {
        // show error
        showError('Please check your numbers and try again.');
        // hide loader
        button.classList.remove('loading');
    }
}

验证测试案例

输入贷款金额1500000、利率2.5%、期限30年:

  • 月利率 = 2.5 / 12 / 100 ≈ 0.00208333
  • 总月数 = 30 * 12 = 360
  • 月供计算结果 ≈ 5926.81,与预期一致

逻辑说明

原代码的单利逻辑是将总利息(本金×年利率×年限)与本金相加后均分至每个月,这不符合实际贷款的复利计息规则。等额本息公式会考虑每月还款后剩余本金产生的利息,因此更贴近真实的贷款还款计算。

内容的提问来源于stack exchange,提问作者SavPhill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 02:45:20