線上運(yùn)行妓雾,可以直接打開:復(fù)利計(jì)算器(在線計(jì)算器)
? ? ? ?作為程序員颅痊,沒有合適的工具横侦,就得手搓一個(gè)顷锰,PC端柬赐,移動(dòng)端均可適用。廢話不多說(shuō)官紫,直接上代碼肛宋。
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">每年復(fù)利次數(shù):</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">投資年數(shù):</label> <input id="years" step="0.01" type="number" placeholder="輸入年數(shù)"><button onclick="calculateCompoundInterest()">計(jì)算</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("請(qǐng)確保所有輸入項(xiàng)都已填寫正確州藕!"); return; } const futureValue = principal * Math.pow(1 + (rate / times), times * years); document.getElementById('future-value').textContent = `未來(lái)投資的價(jià)值: ¥${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; }