組合型房貸 = **公積金貸款 + 商業貸款** 同時申請。本文提供計算公式、前端介面設計、核心邏輯程式碼與進階功能擴展。
月利率 r = 年利率 / 12
月供 M = P * r * (1+r)^n / ((1+r)^n - 1)
總利息 = M * n - P
// P: 貸款本金, n: 總期數(月)
月本金 = P / n
月利息 = 剩餘本金 * r
月供遞減,首月最高
function calculateMixedLoan(params) {
const {
totalPrice, downPaymentRatio,
gjjAmount, gjjRate,
syAmount, syRate,
years, method // 'equal_installment' | 'equal_principal'
} = params;
const n = years * 12;
const r_gjj = ggjRate / 100 / 12;
const r_sy = syRate / 100 / 12;
// 公積金部分
const gjjMonthly = calcMonthly(gjjAmount, r_gjj, n, method);
const gjjTotal = gjjMonthly * n;
const gjjInterest = gjjTotal - gjjAmount;
// 商貸部分
const syMonthly = calcMonthly(syAmount, r_sy, n, method);
const syTotal = syMonthly * n;
const syInterest = syTotal - syAmount;
// 合計
const totalMonthly = gjjMonthly + syMonthly;
const totalInterest = gjjInterest + syInterest;
const totalRepay = gjjTotal + syTotal;
return {
gjj: { monthly: gjjMonthly, total: gjjTotal, interest: gjjInterest },
sy: { monthly: syMonthly, total: syTotal, interest: syInterest },
summary: { monthly: totalMonthly, total: totalRepay, interest: totalInterest }
};
}
function calcMonthly(P, r, n, method) {
if (method === 'equal_principal') return P / n + P * r; // 首月近似
return P * r * Math.pow(1+r, n) / (Math.pow(1+r, n) - 1);
}
| 區塊 | 元素 | 互動 |
|---|---|---|
| 房屋資訊 | 總價、首付比例、貸款年限 | 輸入聯動試算 |
| 貸款結構 | 公積金額度/利率、商貸額度/利率 | 滑桿調整比例、即時顯示月供 |
| 還款方式 | Radio: 等額本息 / 等額本金 | 切換重算 |
| 結果展示 | 月供明細、總利息、總還款、前N年明細表 | 表格可排序、匯出 CSV |
| 進階 | 提前還款模擬、利率調整模擬、稅費試算 | 模態框輸入參數 |
loanCalculator.js 純函式,無 DOM 依賴,易單元測試。debounce(300ms) 觸發試算,避免頻繁重繪。掌握公式核心、分離計算與視圖、提供豐富互動與視覺化,即可打造專業級組合型房貸計算器。