lib/exonio/financial.rb in exonio-0.2.0 vs lib/exonio/financial.rb in exonio-0.3.0

- old
+ new

@@ -20,9 +20,32 @@ fact = (1 + rate* end_or_beginning) * (temp - 1) / rate -(pv * temp + pmt * fact) end + # Calculates the payment on interest for an investment based on + # constant-amount periodic payments and a constant interest rate. + # + # @param rate [Float] The interest rate as decimal (not per cent) per period + # @param per [Integer] The amortization period, in terms of number of periods + # @param nper [Integer] The number of payments to be made + # @param pv [Float] The present value + # @param fv [Float] The future value remaining after the final payment has been made + # @param end_or_begining [Integer] Whether payments are due at the end (0) or + # beggining (1) of each period + # + # @return [Float] + # + # @example + # Exonio.ipmt(0.075 / 12, 8, 12 * 2, 5000) # ==> -22.612926783996798 + # + def ipmt(rate, per, nper, pv, fv = 0, end_or_beginning = 0) + pmt = self.pmt(rate, nper, pv, fv, end_or_beginning) + fv = self.fv(rate, (per - 1), pmt, pv, end_or_beginning) * rate + temp = end_or_beginning == 1 ? fv / (1 + rate) : fv + + (per == 1 && end_or_beginning == 1) ? 0.0 : temp + end # Calculates the number of payment periods for an investment based on # constant-amount periodic payments and a constant interest rate. # # @param rate [Float] The interest rate as decimal (not per cent) per period