Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Should total interest be derived from the rounded or the exact annuity payment?
I run a site with a small annuity loan calculator written in plain JavaScript. It shows two numbers: the monthly payment, and the total interest paid over the term.
The payment comes from the standard annuity formula:
var n = years * 12;
var r = annualRatePercent / 100 / 12;
var f = Math.pow(1 + r, n);
var term = principal * r * f / (f - 1);
The payment is displayed rounded to whole units:
Math.round(term)
but the total interest is derived from the unrounded value:
Math.round(term * n - principal)
So the two displayed figures do not reconcile with each other. For a principal of 300000 at 14 percent over five years, the exact payment is 6980.4753. The page shows 6980 as the payment and 118829 as total interest. A reader who multiplies the displayed payment by 60 and subtracts the principal arrives at 118800, which is 29 lower. At 250000 and 16 percent over five years the error runs the other way, 29 too high.
The amounts are small, but people do check the arithmetic, and numbers that fail to add up look careless on a page about money.
As far as I can tell there are three options:
- Keep the exact payment for the totals and accept that the displayed figures do not reconcile.
- Round the payment first and derive everything from the rounded value, so the page is internally consistent but slightly off from what a lender would actually charge.
- Build an actual amortization schedule, round each instalment, and let the final instalment absorb the remainder.
The third is clearly the most accurate. What I am unsure about is whether it is the right choice for a display-only estimate, because it makes the last payment differ from every other one, and that then has to be explained in the interface.
Is there an established convention here? I would rather follow whatever amortization libraries have settled on than invent a rule of my own.
The deployed version is at https://samlegjeld.no/samle-gjeld-kalkulator/ if seeing the output helps.

1 comment thread