作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><label for="monthlyDeposit">每月存款金额 (元):</label> <input id="monthlyDeposit" type="number" placeholder="输入每月存款金额"> <label for="years">存款年数:</label> <input id="years" type="number" placeholder="输入存款年数"> <label for="interestRate">年利率 (%):</label> <input id="interestRate" step="0.01" type="number" placeholder="输入年利率"><button onclick="calculateRetirement()">计算退休金</button><div id="result" class="result">结果将显示在这里</div></div>
JS:
function calculateRetirement() { const monthlyDeposit = parseFloat(document.getElementById('monthlyDeposit').value); const years = parseFloat(document.getElementById('years').value); const interestRate = parseFloat(document.getElementById('interestRate').value) / 100; if (isNaN(monthlyDeposit) || isNaN(years) || isNaN(interestRate)) { document.getElementById('result').textContent = "请输入有效的数值。"; return; } const n = 12; // 每年存款次数(月) const totalMonths = years * n; let totalAmount = 0; // 循环计算每个月的金额,考虑复利 for (let i = 0; i < totalMonths; i++) { totalAmount += monthlyDeposit * Math.pow(1 + interestRate / n, totalMonths - i); } document.getElementById('result').textContent = `退休时的总存款金额为:${totalAmount.toFixed(2)} 元`; }
CSS:
.calculator { width: 100%; background-color: #333; color: white; padding: 20px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3); } label { display: block; margin-bottom: 10px; font-size: 16px; } input, select { width: 100%!important; padding: 10px!important; margin-bottom: 20px; color: #000000; border-radius: 5px; border: 1px solid #555; font-size: 16px!important; background-color: #ffffff!important; } button { width: 100%; padding: 10px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; } button:hover { background-color: orange; } .result { font-size: 18px; margin-top: 20px; text-align: center; } option { background-color: #ffffff; } p { font-size: 18px; margin-top: 5px!important; }