I've written the "monthly payment" function more times than I'd like to admit. The function that goes the other way, from a payment schedule back to an APR, is the one people tend to get wrong, usually by quietly returning the note rate and calling it APR. Here's how I compute it in plain JavaScript, following the method Regulation Z actually spells out.
The note rate and the APR answer different questions
The note rate tells you how the payment is calculated. The APR tells the borrower what the credit costs once certain upfront charges are counted. The trick is that Reg Z doesn't add fees to the rate; it takes them out of the money you actually received.
Under 12 CFR 1026.18(b), the amount financed starts from the principal and then subtracts "any prepaid finance charge" (CFPB, 1026.18). The payment schedule doesn't change. So the APR is the rate at which that smaller amount financed is exactly paid off by the same payments.
That gives us a clean way to think about the code:
- Compute the payment from the note rate and the full principal.
- Compute the amount financed (principal minus prepaid finance charges).
- Solve for the periodic rate that makes the present value of the payments equal the amount financed.
- Annualize it.
Step 1: the payment, rounded like a real lender would
function monthlyPayment(principal, annualRate, months) {
const i = annualRate / 12;
const exact = principal * i / (1 - Math.pow(1 + i, -months));
return Math.round(exact * 100) / 100; // the borrower pays cents, not floats
}
const principal = 20000;
const payment = monthlyPayment(principal, 0.07, 60); // 396.02
Rounding matters here. The unrounded payment is $396.0240, and the schedule the borrower sees says $396.02. Your APR should be solved against the payment they actually make.
Step 2: present value of the payment stream
Appendix J describes the actuarial method: each unit-period, the unpaid balance of the amount financed grows by the finance charge earned and shrinks by the payment (CFPB, Appendix J). For a regular monthly loan, that's the same as saying the amount financed equals the present value of the payments at the unit-period rate.
function presentValue(i, payment, months) {
if (i === 0) return payment * months;
// (1 - (1+i)^-n) / i, written to stay accurate for tiny i
return payment * -Math.expm1(-months * Math.log1p(i)) / i;
}
Step 3: solve for the rate
Present value falls as the rate rises, so a plain bisection is boring and bulletproof. I start with it and only reach for Newton's method if I need the speed.
function solvePeriodicRate(amountFinanced, payment, months) {
let lo = 0, hi = 1; // 0% to 100% per month brackets any sane loan
for (let k = 0; k < 100; k++) {
const mid = (lo + hi) / 2;
if (presentValue(mid, payment, months) > amountFinanced) lo = mid;
else hi = mid;
}
return (lo + hi) / 2;
}
A hundred halvings of a 0-to-1 interval is far past double precision, so there's no tolerance to tune. Newton's method from a starting guess of the note rate converged in four iterations on the example below, which is nice, but bisection can't diverge on a weird input and Newton can.
Step 4: annualize the way the regulation says
Appendix J's general rule: the APR is the nominal rate "determined by multiplying the unit-period rate by the number of unit-periods in a year." For monthly payments that's i * 12. Not (1 + i) ** 12 - 1. That second formula is an effective annual rate, a legit number, just not the one Reg Z asks for.
const prepaidFinanceCharge = 500; // e.g., an origination fee paid at closing
const amountFinanced = principal - prepaidFinanceCharge; // 19,500
const i = solvePeriodicRate(amountFinanced, payment, 60);
const apr = i * 12 * 100;
console.log(apr.toFixed(3)); // 8.068
So a $20,000, 60-month loan at a 7% note rate with a $500 prepaid finance charge carries an APR of about 8.07%. Same payment, same term; the borrower just walked away with $19,500 of actual credit.
A sanity test that catches a sneaky bug
Run the solver with no fees at all, against the rounded payment:
solvePeriodicRate(20000, 396.02, 60) * 1200; // ≈ 6.9996, not 7.0000
That tiny gap is the two-tenths of a cent you rounded off each month. It's correct behavior. If your test asserts === 7, the test is wrong, not the solver. Assert within a tolerance instead.
How precise does the answer need to be?
Reg Z has an answer for that too. Under 1026.22(a)(2), the APR for a regular transaction is considered accurate if it is within 1/8 of 1 percentage point of the actual rate; for an irregular transaction, 1026.22(a)(3) allows 1/4 of 1 percentage point (CFPB, 1026.22). That's the tolerance for a disclosure, not a license to be sloppy in code. I still solve to full precision and display two or three decimals. But it's a useful yardstick when you're comparing your output against a lender's paperwork and you're off in the third decimal.
Things this sketch deliberately skips
- Odd first periods. If the first payment isn't exactly one month out, Appendix J has rules for fractional unit-periods, and the simple annuity formula no longer applies.
- Which fees count. Deciding what's a finance charge is a legal question (1026.4), not a math one. Make it an explicit input, never a guess.
- Variable rates and mortgages. There are extra rules for both. Use this for intuition and simple installment loans, not compliance software.
I keep these four steps in mind whenever I touch the loan math in FINORA, my set of browser-based loan, compound interest, and ROI calculators. If you've built an APR solver and hit an edge case I skipped, tell me in the comments. I'm collecting them.
Top comments (0)