I've shipped a few interest calculators, and the bugs that bit me were rarely about rounding cents. They came from converting rates: annual to monthly, nominal to effective, APR to "the rate I should actually plug in." Here are the four I see most in code reviews, with small fixes in plain JavaScript.
1. Treating APR, the note rate, and APY as the same number
Three different numbers get called "the interest rate," and a calculator that mixes them up will be confidently wrong.
- The note rate (what most loan formulas want) is the cost of borrowing per year, before fees.
- APR is broader. The CFPB explains that a mortgage APR "reflects the interest rate, any points, mortgage broker fees, and other charges," which is why it's usually higher than the interest rate (CFPB).
- APY is a savings-side number that includes compounding. Regulation DD defines it as reflecting "the interest rate and the frequency of compounding," annualized on a 365-day year (12 CFR 1030, Appendix A).
If you let a user type "APR" into a field and then feed it into an amortization formula as the note rate, your monthly payment will come out too high, because fees are now being charged interest. My rule: label the input explicitly ("interest rate, not APR") and keep the APR as a separate, derived output if you show it at all.
2. Forgetting to convert the annual rate to a per-period rate
This one looks silly written down, but I've caught it in production code:
const pmt = (principal, ratePerPeriod, periods) =>
principal * ratePerPeriod / (1 - Math.pow(1 + ratePerPeriod, -periods));
pmt(200000, 0.065 / 12, 360); // 1264.14 correct-ish monthly payment
pmt(200000, 0.065, 360); // 13000.00 annual rate used as monthly
The formula doesn't care what a "period" is. If periods is months, the rate must be per month too. The cheap guard is a unit in the variable name (monthlyRate, termMonths) and a sanity test: a $200k, 30-year loan at 6.5% should land near $1,264, not $13,000.
3. Dividing by 12 when the source rate is already effective
annualRate / 12 is right when the annual number is a nominal rate compounded monthly, which is the usual convention for US mortgages. It's wrong when the annual number is an effective rate like an APY or an "annual return" that a user typed in for an ROI projection.
const annualEffective = 0.06;
const naive = annualEffective / 12; // 0.005
const exact = Math.pow(1 + annualEffective, 1 / 12) - 1; // 0.0048675...
The gap looks tiny, but compounded over 30 years of monthly contributions it adds up to a visibly inflated balance. The same confusion runs the other way when you display APY from a nominal rate:
const apy = (nominal, periodsPerYear) =>
Math.pow(1 + nominal / periodsPerYear, periodsPerYear) - 1;
apy(0.06, 12); // 0.06168 -> 6.17%
apy(0.06, 365); // 0.06183 -> 6.18%
Those two results happen to match the worked examples in Regulation DD's appendix, which computes 6.17% for $61.68 earned on $1,000 over 365 days and 6.18% for $30.37 earned on a 182-day CD, using APY = 100 [(1 + Interest/Principal)^(365/Days in term) - 1]. That formula is handy as a test fixture: if your code can't reproduce the regulator's own examples, something is off.
4. Losing precision when the per-period rate is tiny
Daily compounding, or hourly accrual on a large balance, means computing (1 + r)^n where r is very small and n is big. Doubles carry about 15 significant digits, and MDN notes that 1 + 1e-16 is exactly 1 in this arithmetic, so the low digits of r vanish the moment you add 1 (MDN: Math.log1p).
Math.log1p and Math.expm1 exist for exactly this (MDN: Math.expm1):
const r = 1e-12, n = 1e6;
Math.pow(1 + r, n) - 1; // 0.0000010000894 (wrong after 4 digits)
Math.expm1(n * Math.log1p(r)); // 0.0000010000005 (accurate)
// a growth-factor helper that stays accurate for small rates
const growth = (r, n) => Math.expm1(n * Math.log1p(r)); // returns (1+r)^n - 1
For everyday monthly mortgage math you won't notice the difference. But if your app lets people model daily compounding on savings, or runs the same function inside a solver (say, backing out an APR with Newton's method), the small-rate error becomes noise that slows or derails convergence. Using the log1p/expm1 form costs nothing and removes the problem.
A short checklist
- Name every rate with its unit and type:
nominalAnnual,effectiveAnnual,monthlyRate. - Convert once, at the boundary, with a single helper. Don't scatter
/ 12across files. - Keep APR as an output that includes fees, never as the input to the payment formula.
- Use
Math.log1p/Math.expm1for growth factors with small rates. - Turn published worked examples (like the Reg DD ones) into unit tests.
I ran into most of these while building FINORA, a set of browser-based compound interest, loan, and ROI calculators, and the checklist above came straight out of those bug fixes. If you've hit a rate-conversion bug I didn't list, I'd love to hear about it in the comments.
Top comments (0)