pub fn amort_schedule_into(
slice: &mut [AmortizationPeriod],
rate: Decimal,
principal: Decimal,
pmt: Decimal,
round: Option<(u32, RoundingStrategy)>,
)
Expand description
Amortization Schedule Into
Calculates the amortization schedule for a loan or mortgage, mutating a slice of AmortizationPeriod
.
The amortization schedule includes a series of payments that are applied to both principal and interest. Each payment reduces the principal balance and pays interest charges based on the remaining balance and the interest rate.
§Arguments
slice
- A mutable slice ofAmortizationPeriod
instances to be filled with the amortization schedule.
Warning: The length of the slice should be as long as the number of periods (e.g. 30 * 12 for 12 months over 30 years) or there will be unexpected behavior.
rate
- The interest rate per periodprincipal
- The present value or principal amount of the loan (should be positive as cash inflow for a mortgage/loan)pmt
- The payment amount per period (should be negative as cash outflow, can be calculated usingpmt
function)round
(optional) - A tuple specifying the number of decimal places and a rounding strategy for the amounts(dp, RoundingStrategy)
, default is no rounding of calculations. The final principal payment is adjusted to zero out the remaining balance if rounding is enabled.rust_decimal::RoundingStrategy::MidpointNearestEven
(“Bankers Rounding”) is likely what you are looking for
§Examples
- 5% rate, 30 year term (360 months), $1,000,000 loan, $4,000 monthly payment
use rust_finprim::amort_dep_tax::{AmortizationPeriod, amort_schedule_into};
use rust_finprim::tvm::pmt;
use rust_decimal_macros::dec;
let nper = 30 * 12;
let rate = dec!(0.05) / dec!(12);
let principal = dec!(1_000_000);
let pmt = pmt(rate, nper.into(), principal, None, None);
let mut schedule = vec![AmortizationPeriod::default(); nper as usize];
amort_schedule_into(schedule.as_mut_slice(), rate, principal, pmt, None);
If you wanted to add an “initial period” to the schedule efficiently, you could do something like this:
use rust_finprim::amort_dep_tax::{AmortizationPeriod, amort_schedule_into};
use rust_finprim::tvm::pmt;
use rust_decimal_macros::dec;
let nper = 30 * 12;
let rate = dec!(0.05) / dec!(12);
let principal = dec!(1_000_000);
let pmt = pmt(rate, nper.into(), principal, None, None);
let mut schedule = vec![AmortizationPeriod::default(); nper as usize + 1];
schedule[0] = AmortizationPeriod::new(0, dec!(0), dec!(0), principal);
amort_schedule_into(&mut schedule[1..], rate, principal, pmt, None);