JSFiddle - React, Tailwind, and code Playground
HTML
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h2>利息差計算</h2>
<div class="card">
<label>本金</label>
<input type="number" id="principal" inputmode="numeric" placeholder="例如 200000">
<label>天數</label>
<div class="days">
<button onclick="setDays(30)" id="d30">30 天</button>
<button onclick="setDays(31)" id="d31">31 天</button>
</div>
<button class="main-btn" onclick="calculate()">計算</button>
</div>
<div class="card">
<h3>A 利息 (1.5%)</h3>
<div id="detailA"></div>
<div class="big-number" id="resultA">0</div>
</div>
<div class="card">
<h3>B 利息</h3>
<div id="detailB"></div>
<div class="big-number" id="resultB">0</div>
</div>
<div class="card highlight">
<h3>差額 B - A</h3>
<div class="big-number" id="diff">0</div>
</div>
CSS
body {
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
margin: 0;
padding: 15px;
background: #f2f4f7;
}
h2 {
text-align: center;
margin-bottom: 15px;
}
.card {
background: white;
padding: 15px;
margin-bottom: 12px;
border-radius: 12px;
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}
.highlight {
background: #eaf7ff;
}
label {
display: block;
margin-top: 10px;
margin-bottom: 5px;
font-weight: bold;
}
input {
width: 100%;
padding: 10px;
font-size: 16px;
border-radius: 8px;
border: 1px solid #ccc;
box-sizing: border-box;
}
/* 天數按鈕 */
.days {
display: flex;
gap: 10px;
}
.days button {
flex: 1;
padding: 10px;
font-size: 16px;
border-radius: 8px;
border: 1px solid #ccc;
background: #fff;
}
.days button.active {
background: #007bff;
color: white;
border: none;
}
/* 主按鈕 */
.main-btn {
width: 100%;
margin-top: 15px;
padding: 12px;
font-size: 18px;
border: none;
border-radius: 10px;
background: #28a745;
color: white;
}
/* 大數字 */
.big-number {
font-size: 28px;
font-weight: bold;
text-align: right;
margin-top: 10px;
}
/* 計算式 */
.formula {
font-size: 13px;
color: #555;
margin: 4px 0;
word-break: break-all;
}
JavaScript
let selectedDays = 30;
function setDays(days) {
selectedDays = days;
document.getElementById("d30").classList.remove("active");
document.getElementById("d31").classList.remove("active");
document.getElementById("d" + days).classList.add("active");
}
function format(num) {
return num.toLocaleString();
}
function calculate() {
const principal = Number(document.getElementById("principal").value);
const days = selectedDays;
if (!principal || principal <= 0) {
alert("請輸入本金");
return;
}
// ===== A =====
let rawA = principal * 0.015 * days / 365;
let interestA = Math.round(rawA);
document.getElementById("detailA").innerHTML = `
<div class="formula">${format(principal)} × 1.5% × ${days} / 365 = ${rawA.toFixed(2)}</div>
<div class="formula">→ ${interestA}</div>
`;
// ===== B =====
let remaining = principal;
let totalB = 0;
let html = "";
let tier1 = Math.min(remaining, 150000);
let calc1 = tier1 * 0.00875 * days / 365;
totalB += calc1;
remaining -= tier1;
if (tier1 > 0) {
html += `<div class="formula">${format(tier1)} × 0.875% × ${days}/365 = ${calc1.toFixed(2)}</div>`;
}
if (remaining > 0) {
let tier2 = Math.min(remaining, 150000);
let calc2 = tier2 * 0.02 * days / 365;
totalB += calc2;
remaining -= tier2;
html += `<div class="formula">${format(tier2)} × 2% × ${days}/365 = ${calc2.toFixed(2)}</div>`;
}
if (remaining > 0) {
let calc3 = remaining * 0.016 * days / 365;
totalB += calc3;
html += `<div class="formula">${format(remaining)} × 1.6% × ${days}/365 = ${calc3.toFixed(2)}</div>`;
}
let interestB = Math.round(totalB);
html += `<div class="formula">合計 ${totalB.toFixed(2)} → ${interestB}</div>`;
document.getElementById("detailB").innerHTML = html;
// ===== 差額 =====
let diff = interestB - interestA;
document.getElementById("resultA").innerText =...