DEV Community

meng meng
meng meng

Posted on Originally published at meng4099244-ship-it.github.io

Loan Calculator: Working Out Monthly Payments and Total Interest Before You Sign

Last month I sat in a dealership while the finance manager slid a printout across the desk: $25,000 financed, 36 months, 6.5% APR, monthly payment $795. Something felt off. I've written enough amortization code to know the rough ballpark for those numbers, and $795 wasn't in it.

I didn't argue from gut feeling. I opened a laptop, ran the math, and came back with $766.23. That's a $28.77 difference every month, or $1,035.72 over the life of the loan. He went to "check with his manager" and the number quietly changed.

Here's the exact process I used, plus the three mistakes that most often corrupt this kind of math. I've hit all three myself.

Method 1: the amortization formula in Python

The standard loan payment formula takes the principal, the monthly rate, and the number of payments. In Python 3.11 it fits in a dozen lines:

# Python 3.11.1
def monthly_payment(principal: float, annual_rate: float, months: int) -> float:
    r = annual_rate / 12          # monthly rate
    return principal * r * (1 + r) ** months / ((1 + r) ** months - 1)

P, apr, n = 25000, 0.065, 36

m = monthly_payment(P, apr, n)
print(f"Monthly payment: {m:.2f}")
print(f"Total paid:      {m * n:.2f}")
print(f"Total interest:  {m * n - P:.2f}")
Enter fullscreen mode Exit fullscreen mode

Running it prints:

Monthly payment: 766.23
Total paid:      27584.10
Total interest:  2584.10
Enter fullscreen mode Exit fullscreen mode

So $25,000 at 6.5% APR over 36 months costs $766.23 a month and $2,584.10 in interest total. This is the standard amortization formula, the same one laid out on Investopedia's amortization page; I also cross-checked the APR definition against the CFPB's explanation of APR to make sure I was comparing like with like.

The term length matters more than people expect. Change n to 72 and the payment drops to $420.25, but total interest jumps to $5,257.87. That's $2,673.77 extra for the privilege of smaller monthly checks. Whenever a salesperson stretches the term to make a payment look affordable, this is the number to run.

Method 2: the spreadsheet you already have open

If you don't want code, a spreadsheet does the same thing in one cell. In Excel, Google Sheets, or LibreOffice Calc:

=PMT(6.5%/12, 36, -25000)
Enter fullscreen mode Exit fullscreen mode

That returns 766.23. Note the minus sign in front of 25000. I'll come back to that, because it trips up almost everyone the first time.

The spreadsheet route is faster for what-if questions. What's the payment at 60 months? Swap the middle argument and you get the answer before the code editor even finishes opening.

Method 3: a browser tool when you just need the number

Both methods above assume you're at a machine where you can run Python or open a spreadsheet. At the dealership I was, but at the bank's front desk last week I wasn't. For those moments I keep a free online loan calculator bookmarked — utilvy's version runs entirely in the browser, needs no signup, and shows monthly payment, total interest, and total cost as you type.

I typed in 25000 / 6.5% / 36 and got $766.23, matching my Python output to the cent. Since everything is client-side, it also works offline once the page is loaded, which mattered more than I expected in a building with terrible cell reception. When I later wanted to compare three term lengths side by side, this browser-based tool made it a matter of editing one field and reading two numbers, no terminal history to scroll through.

Three mistakes that silently corrupt the math

Mistake 1: feeding the annual rate where the monthly rate belongs. Forget to divide by 12 and nothing crashes — you just get a confidently wrong number. Here's the same call with the annual rate used directly:

>>> monthly_payment(25000, 6.5, 36)   # annual rate, not divided by 12
1812.833011314213
Enter fullscreen mode Exit fullscreen mode

$1,812.83 instead of $766.23. The formula is happy to accept any rate you give it, so the only defense is sanity-checking the output. If a 3-year payment on a $25k loan comes out higher than the loan itself, look at the rate first.

Mistake 2: fighting the spreadsheet's sign convention. Finance functions treat the loan as money flowing from the bank to you and the payment as money flowing back, so directions get opposite signs. This cell returns a negative number:

=PMT(6.5%/12, 36, 25000)   →   -766.23
Enter fullscreen mode Exit fullscreen mode

The first time I saw that, I assumed the loan terms were wrong and spent twenty minutes re-checking the paperwork. The payment was fine; the sign was. Two money flows in opposite directions, one negative. Prefix the principal with a minus, as in Method 2, and the result comes out positive.

Mistake 3: rounding inside the amortization loop. If you build a full payment schedule row by row and round each line to cents as you go, the errors accumulate. I tried it on the same loan and the final balance came out at -0.20 instead of 0 — the schedule drifted twenty cents into over-payment, and total interest landed at 2584.08 instead of 2584.10. Small amounts, but anyone auditing the schedule against the lender's will notice. Keep full precision in the loop and round only for display.

One more thing worth flagging: the note rate and the APR are not the same number when fees are involved. Repricing the same loan at a 6.72% APR (fees folded in) gives $768.73 a month rather than $766.23. Lenders quote whichever number makes the offer look better, so compute with both.

Summary

Method Best for Result on this loan
Python amortization script Exact math, schedules, automation $766.23 / $2,584.10 interest
Spreadsheet PMT Quick what-if comparisons $766.23, watch the sign
utilvy's loan calculator No terminal, no spreadsheet, offline-friendly $766.23, matches to the cent

Whichever route you take, the payment number is the least interesting part. Total interest is what tells you whether the loan terms are actually good, and it's one field away in every method above.

More free browser-based utilities at utilvy.com.

Top comments (0)