Your Amortization Loop Never Reaches Zero: 4 Bugs in Loan Math Code
javascript
dev.to
If you've ever generated an amortization schedule by hand, you've probably hit this: you loop 360 times and the balance lands on... -$3.07. Or +$0.84. Never exactly zero. Here are four bugs I keep running into, with numbers you can check yourself. 1. The loop's last step is a lie $300,000 at 7% over 360 months. The exact payment is 1,995.9074..., which you round to 1,995.91. Run balance = balance + balance*r - payment 360 times and the balance ends at -$3.07 - you overpaid by three