作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class="calculator"><div class="form"><label for="principal">本金 (元):</label> <input id="principal" step="0.01" type="number" placeholder="输入本金"> <label for="rate">年利率 (%):</label> <input id="rate" step="0.01" type="number" placeholder="输入年利率"> <label for="times">每年复利次数:</label><select id="times"><option value="1">每年 1 次</option><option value="2">每年 2 次</option><option value="4">每年 4 次</option><option value="12">每年 12 次</option><option value="365">每年 365 次</option></select><label for="years">投资年数:</label> <input id="years" step="0.01" type="number" placeholder="输入年数"><button onclick="calculateCompoundInterest()">计算</button><div class="result"><p id="future-value"></p></div></div></div>
JS:
function calculateCompoundInterest() { const principal = parseFloat(document.getElementById('principal').value); const rate = parseFloat(document.getElementById('rate').value) / 100; const times = parseInt(document.getElementById('times').value); const years = parseFloat(document.getElementById('years').value); if (isNaN(principal) || isNaN(rate) || isNaN(times) || isNaN(years)) { alert("请确保所有输入项都已填写正确!"); return; } const futureValue = principal * Math.pow(1 + (rate / times), times * years); document.getElementById('future-value').textContent = `未来投资的价值: ¥${futureValue.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; display: block; margin: 0px; margin-bottom: 20px; margin-left: 0px!important; } button:hover { background-color: orange; } .result { margin-top: 20px; text-align: center; } option { background-color: #ffffff; } p { font-size: 18px; margin-top: 5px!important; }