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

如何制作基金定投(SIP)计算器?已完成HTML CSS基础版却不知计算逻辑

搞定基金定投计算器的核心计算逻辑

嘿,我懂你现在的状态——UI界面已经搭得有模有样,但核心的复利计算逻辑卡壳了对吧?别慌,基金定投(SIP)的计算有标准的复利公式,咱们一步步把它落地到代码里。

先搞懂SIP的核心计算公式

基金定投的到期总金额是用复利公式计算的,核心公式是:

M = P × [( (1 + r)^n - 1 ) / r] × (1 + r)

我给你拆解下每个变量的意思:

  • M:到期后的总金额(本金+收益)
  • P:每期定投的固定金额(比如每月投5000元)
  • r:每期的利率(划重点!如果是年利率,每月定投的话要除以12,还要转成小数,比如年化12%就是12%/12/100=0.01)
  • n:定投的总期数(比如定投10年,每月投一次就是12×10=120期)

如果遇到利率为0的情况(比如纯攒钱无收益),直接用总金额=每期金额×总期数就行,避免除以0的错误。

把逻辑落地到JavaScript代码里

假设你已经有了基础的HTML结构,我给你补全核心的JS计算逻辑,直接就能用:

第一步:补全HTML的关键元素(如果还没加)

先确保你的页面有输入框、计算按钮和结果展示区:

<div class="sip-calculator">
  <h2>基金定投计算器</h2>
  <div class="input-group">
    <label>每期定投金额 (元):</label>
    <input type="number" id="monthlyInvestment" min="1" value="5000">
  </div>
  <div class="input-group">
    <label>预期年化收益率 (%):</label>
    <input type="number" id="annualRate" min="0" step="0.1" value="12">
  </div>
  <div class="input-group">
    <label>定投期限 (年):</label>
    <input type="number" id="investmentYears" min="1" value="10">
  </div>
  <button onclick="calculateSIP()">计算到期金额</button>
  <div class="result">
    <h3>到期总金额:</h3>
    <p id="totalAmount">-</p>
    <p id="totalInvested">-</p>
    <p id="totalReturns">-</p>
  </div>
</div>

第二步:编写核心计算函数

把下面的JS代码放到你的页面里(比如<script>标签或者单独的JS文件):

function calculateSIP() {
  // 1. 获取用户输入的数值
  const monthlyInvestment = parseFloat(document.getElementById('monthlyInvestment').value);
  const annualRate = parseFloat(document.getElementById('annualRate').value);
  const investmentYears = parseFloat(document.getElementById('investmentYears').value);

  // 2. 验证输入是否有效(避免非法值导致计算错误)
  if (isNaN(monthlyInvestment) || monthlyInvestment <= 0 ||
      isNaN(annualRate) || annualRate < 0 ||
      isNaN(investmentYears) || investmentYears <= 0) {
    alert('请输入有效的数值哦!');
    return;
  }

  // 3. 转换计算参数:年化利率转月利率,年数转总月数
  const monthlyRate = annualRate / (12 * 100); // 转成小数形式的月利率
  const totalMonths = investmentYears * 12;

  // 4. 计算到期总金额
  let totalAmount;
  if (monthlyRate === 0) {
    // 利率为0时直接算总本金,避免除以0的错误
    totalAmount = monthlyInvestment * totalMonths;
  } else {
    const compoundFactor = Math.pow(1 + monthlyRate, totalMonths);
    totalAmount = monthlyInvestment * ((compoundFactor - 1) / monthlyRate) * (1 + monthlyRate);
  }

  // 5. 计算总投入和总收益
  const totalInvested = monthlyInvestment * totalMonths;
  const totalReturns = totalAmount - totalInvested;

  // 6. 格式化金额(加千分位,保留两位小数)
  const formatCurrency = (amount) => {
    return amount.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  };

  // 7. 更新页面上的结果展示
  document.getElementById('totalAmount').textContent = `¥${formatCurrency(totalAmount)}`;
  document.getElementById('totalInvested').textContent = `累计投入本金: ¥${formatCurrency(totalInvested)}`;
  document.getElementById('totalReturns').textContent = `累计收益: ¥${formatCurrency(totalReturns)}`;
}

一些优化小建议

如果想让你的计算器更专业,可以加这些功能:

  • 实时计算:不用点击按钮,监听输入框的input事件,用户输入时自动更新结果
  • 步增SIP支持:允许用户设置每年增加一定比例的定投金额(比如每年多投10%)
  • 可视化展示:用简单的Canvas或者图表库画本金和收益的增长曲线
  • 通胀调整:加入通胀率参数,计算实际购买力

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:03:57