线上运行,可以直接打开:利率计算器(在线计算器)
作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。
HTML:
<div class=”calculator”><input id=”principal” type=”number” placeholder=”输入本金 (元)”> <input id=”rate” type=”number” placeholder=”输入年利率 (%)”> <input id=”time” type=”number” placeholder=”输入时间 (年)”><button onclick=”calculateInterest()”>计算利息</button><div id=”result” class=”result”></div></div>
JS:
function calculateInterest() {
const principal = parseFloat(document.getElementById(‘principal’).value);
const rate = parseFloat(document.getElementById(‘rate’).value);
const time = parseFloat(document.getElementById(‘time’).value);if (isNaN(principal) || isNaN(rate) || isNaN(time) || principal <= 0 || rate <= 0 || time <= 0) {
document.getElementById(‘result’).innerText = ‘错误:请输入有效的本金、利率和时间。’;
return;
}const interest = (principal * rate * time) / 100; // 利息 = 本金 * 利率 * 时间
const totalAmount = principal + interest;
document.getElementById(‘result’).innerText = `利息: ${interest.toFixed(2)} 元, 总金额: ${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;
}
暂无评论内容