The EMI formula fits on one line. I still shipped a loan calculator whose last month could leave a few paise unpaid, and the table quietly hid them by clamping the balance to zero.
That calculator was a plain-JavaScript page on Calculator Junction, a free site with a few hundred calculators. When I ported the home loan page to TypeScript for an Astro rebuild, I wrote down everything the old version got wrong in a comment at the top of the new file. This post walks through the result: the formula, the edge cases, and the tests that keep it honest.
You can try the finished thing on the home loan EMI calculator. Every snippet below is copied from the code behind it.
Key takeaways
- The standard EMI formula breaks at 0% interest. Branch to
P / ninstead of dividing by zero.- Don't trust the running balance to land on zero. Make the last instalment pay off whatever is left.
- Decide where rounding happens. Rounding every month to paise left 56 paise unpaid on a 20-year loan in my tests.
- Test against numbers you can check by hand or against a bank, not against your own previous output.
The formula, and what each letter means
An EMI (equated monthly instalment) is a fixed payment that clears a loan with interest over n months. The usual form is:
EMI = P × r × (1 + r)^n / ((1 + r)^n − 1) Two of these trip people up:
-
ris the monthly rate as a fraction. An 8.5% annual rate becomes8.5 / 12 / 100 = 0.0070833. -
nis in months. A 20-year loan is 240, not 20.
Here's the production function:
/** Fixed monthly instalment of an annuity loan; at 0 % the principal is spread evenly. */ export function emi(P: number, annualRate: number, n: number) { const r = annualRate / 1200; if (!(P > 0) || !(n > 0)) return NaN; return r > 0 ? (P * r * (1 + r) ** n) / ((1 + r) ** n - 1) : P / n; } annualRate / 1200 does the ÷ 12 ÷ 100 in one step. The guard uses !(P > 0) rather than P <= 0 on purpose: it also catches NaN, which is what an empty input field turns into.
Edge case 1: a 0% loan divides by zero
Interest-free loans are common: "no-cost EMI" phone deals, loans from family, some employer advances. At r = 0 the formula becomes 0 × 1 / (1 − 1), which is 0 / 0, which is NaN.
The maths has a clean answer. With no interest you just split the principal evenly, so the code branches to P / n. That's the r > 0 ? ... : P / n at the end of the function above.
Splitting evenly isn't the end of it, because money is paid in whole paise. ₹1,000 over 7 months is ₹142.857142... a month. Round each payment to ₹142.86, and seven of them add up to ₹1,000.02. So six payments are ₹142.86 and the seventh is ₹142.84. Our amortization calculator's test suite pins exactly that:
// 1000 at 0 % over 7 months near('rate 0 payment', r.regular, 142.86); near('rate 0 last payment', r.last, 142.84); Edge case 2: the balance that never quite reaches zero
A repayment schedule is a loop: each month, take interest on the balance, subtract the rest of the EMI from the principal, repeat. It looks harmless:
for (let i = 0; i < n; i++) { const interest = balance * r; const principal = emi - interest; balance -= principal; } With pure floats the drift is tiny: run that for ₹25 lakh at 8.5% over 240 months and the final balance is -4.19e-9. Add any rounding to the EMI or to the monthly figures and the leftover grows to paise, which is what the old page had. It papered over it by clamping the balance at zero, so the table looked right while the payments didn't add up to the loan.
The fix is one line. The last instalment pays whatever is left, so the loop can't drift:
let principal = method === 'emi' ? fixed - interest : flatPrincipal; if (i === n - 1) principal = balance; // the last instalment clears what is left (no float residue) Edge case 3: decide where rounding lives
Floating-point drift is tiny. Rounding drift isn't.
I tried the "obvious" bank-style version: round the EMI to paise, round each month's interest to paise, carry the rounded balance forward. After 240 months on the same ₹25 lakh loan, ₹0.56 was still unpaid. Lenders usually deal with this by adjusting the final instalment, and that's what our dedicated amortization calculator does. It works in whole paise and lets the last row absorb the difference.
The home loan page takes the other route. It keeps full precision through the whole schedule and rounds only for display (and in the CSV export, via toFixed(2)). Both approaches are fine. What matters is picking one deliberately and putting it in the last-row logic, rather than finding out from a user.
The schedule: where the money actually goes
The EMI stays the same every month, but the split inside it doesn't. Early on, almost all of it is interest.
Numbers from the same function, ₹25 lakh at 8.5% for 20 years:
| Value | |
|---|---|
| EMI | ₹21,696 |
| Interest in the first EMI | ₹17,708 (81.6%) |
| Half the principal repaid | month 166, in the 14th year |
| Total interest | ₹27,06,939 |
This is the chart I'd show anyone who is about to take a long loan. It's also why part-prepayments in the early years save so much: every rupee of principal you clear early stops collecting 8.5% for the rest of the term. Our prepayment calculator puts a number on that for your own loan.
Equal principal: the other way banks structure loans
Not every loan is an annuity. In an equal-principal loan (called SAC in Brazil, where it's common for mortgages) you repay the same slice of principal every month. Interest falls as the balance falls, so the instalment starts high and shrinks.
The schedule function takes a method of 'emi' | 'sac'. For SAC the principal is a flat P / n. Totals need a different formula too, because there's no fixed instalment to multiply:
const totalPayment = method === 'emi' ? fixed * n : P + months.reduce((s, m) => s + m.interest, 0); On our example loan, equal principal starts at ₹28,125 a month (₹6,429 more than the EMI) but costs ₹21,33,854 in interest instead of ₹27,06,939. That's ₹5,73,085 less, in exchange for heavier early payments.
Try it
The home loan EMI calculator runs exactly the code above. Change the rate or tenure and the totals, the principal-vs-interest chart and the year-by-year schedule update as you type. Switch the repayment to "Equal principal" to see the SAC numbers, or switch the schedule to "Financial year (Apr–Mar)" to see the grouping from the next section. There's a CSV export of the full monthly schedule if you want to check the arithmetic yourself.
Grouping by Indian financial year
Indian tax runs April to March, so people want "how much interest did I pay in FY 2026-27?", not "in 2026". Section 24(b) and 80C claims are per financial year. The grouping is two lines:
const key = fy ? (m.month >= 3 ? m.year : m.year - 1) : m.year; const label = fy ? `FY ${key}-${String((key + 1) % 100).padStart(2, '0')}` : String(key); m.month is zero-based like JavaScript's Date, so >= 3 means April onward. A payment in February 2027 belongs to FY 2026-27. The % 100 with padStart turns 2027 into 27 and 2100 into 00.
A numerically nicer formula for tiny rates
Our separate amortization calculator writes the same formula differently:
return (principal * monthlyRate) / -Math.expm1(-n * Math.log1p(monthlyRate)); This is algebraically identical to the textbook form (P·r / (1 − (1+r)^−n)). Math.log1p(r) and Math.expm1(x) exist for exactly this kind of problem. When r is very small, computing 1 + r throws away the low digits of r before the power is even taken. log1p and expm1 keep them. You won't notice the difference at 8.5%. You might at 0.01%, or when a user types a rate in basis points by mistake.
Testing a calculator without fooling yourself
The easy trap is snapshot testing: run the code, save its output, assert it never changes. That catches regressions but happily locks in a wrong answer.
Our amortization tests use numbers that come from outside the code: values you can check by hand, or against a bank's published EMI. A few of the 233 checks:
near('250k 6.5% 30y', R({}).regular, 1580.17); near('100k 6% 15y', R({ amount: 100000, rate: 6, years: 15 }).regular, 843.86); near('first interest = 250000 x 0.065/12', r.rows[0].interest, 1354.17); The first-month interest check is my favourite. It's the one number anyone can verify on a phone calculator in ten seconds (250,000 × 0.065 ÷ 12), and if it's wrong, nothing after it can be right.
What I'd tell myself before writing the first version
- Branch on
r === 0before you write anything else. - Make the last instalment absorb whatever is left.
- Choose where rounding happens, write it down, and test the last row.
- Test against numbers from outside your code.
- If your users pay tax on an April–March year, give them that view. It's two lines.
All of this runs in the browser, with no sign-up, on Calculator Junction's home loan EMI calculator. If you build one yourself and find a case I missed, I'd like to hear about it in the comments.

