← Back to Home
Calculator
// 迭代法求解APR
function calcAPRAdv() {
const amount = parseFloat(document.getElementById('amount').value);
const fee = parseFloat(document.getElementById('fee').value);
const payment = parseFloat(document.getElementById('payment').value);
const periods = parseInt(document.getElementById('periods').value);
if ([amount, fee, payment, periods].some(x => isNaN(x)) || amount <= 0 || payment <= 0 || periods <= 0) {
document.getElementById('result').textContent = 'Please enter valid values';
return;
}
// cash flow:t=0为-(amount-fee),t=1~n为payment
// Target:NPV=0,求monthInterest Rater
let pv = amount - fee;
let low = 0, high = 1, r = 0;
let npv = 0;
for (let iter = 0; iter < 100; iter++) {
r = (low + high) / 2;
npv = 0;
for (let i = 1; i <= periods; i++) {
npv += payment / Math.pow(1 + r, i);
}
if (npv > pv) {
low = r;
} else {
high = r;
}
if (Math.abs(npv - pv) < 1e-8) break;
}
const apr = r * 12 * 100;
document.getElementById('result').textContent = `Actual yearInterest Rate(APR)approx:${apr.toFixed(4)}%`;
}