Compound interest
The WebAssembly module exports a compound interest function that takes a principal, an annual rate expressed as a percentage, a number of years, and a number of compounding periods per year, and returns the final balance. It applies the standard compound interest formula, with the rate divided by 100 to convert the percentage into a fraction. Like the percentage function it is a free function with no keypad button, available to any JavaScript that imports the module.
The four arguments
- principal — the starting balance.
- rate — the annual rate as a percentage, so 5 means five percent, not 500 percent.
- years — the number of years, which may be fractional.
- compounds_per_year — 12 for monthly, 4 for quarterly, 1 for annual.
The function returns the final balance, not the interest earned. Subtract the principal from the result to get the interest on its own.
Worked examples
These figures are the ones the crate's unit tests assert, so they describe what the function actually returns rather than what the formula ought to give.
- 1000 at 5 percent for 10 years compounded monthly returns approximately 1647.01.
- 1000 at 10 percent for 5 years compounded annually returns approximately 1610.51.
- A principal of 0 returns exactly 0, whatever the rate.
- A rate of 0 returns the principal unchanged.
- A term of 0 years returns the principal unchanged.
Questions and answers
How do I enter compound interest on the calculator keypad?
You cannot. The function takes four arguments and the keypad has one display and one memory register, so it has no button. The function is exported by the WebAssembly module for direct use from JavaScript.
Should the rate be entered as 5 or as 0.05?
As 5. The function expects the annual rate as a percentage and divides it by 100 internally.
Does the function return the interest earned or the final balance?
The final balance. Subtract the principal from the result to get the interest on its own.