class FinancialCalculator::Pmt

Calculates the payment of an ordinary annuity

Attributes

future_value[R]

@return [Numeric] The ending value of the annuity. Defaults to 0 @api public

num_periods[R]

@return [Numeric] The number of payments to be made @api public

pay_at_beginning[R]

@return [Boolean] Whether the payment is made at the beginning of the

period (true) or end of the period (false)

@api public

present_value[R]

@return [Numeric] The current value of the annuity @api public

rate[R]

@return [Numeric] The rate used for calculating the payment amount @api public

result[R]

@return [DecNum] Result of the PMT calculation @api public

Public Class Methods

new(rate, num_periods, present_value, future_value = 0, pay_at_beginning = false) click to toggle source

Create a new object for calculating the periodic payment of an ordinary annuity @param [Numeric] rate The discount (interest) rate @param [Numeric] num_periods The number of payments to be made @param [Numeric] present_value The current value of the annuity @param [Numeric] future_value The ending value of the annuity @param [Boolean] pay_at_beginning. Whether the payment is made at the beginning

of the period (true) or end of the period (false)

@return [FinancialCalculator::Npv] An instance of a PMT calculation

# File lib/financial_calculator/pmt.rb, line 39
def initialize(rate, num_periods, present_value, future_value = 0, pay_at_beginning = false)
  validate_numerics(rate: rate, num_periods: num_periods, present_value: present_value, future_value: future_value)

  @rate             = Flt::DecNum(rate.to_s)
  @num_periods      = Flt::DecNum(num_periods.to_s)
  @present_value    = Flt::DecNum(present_value.to_s)
  @future_value     = Flt::DecNum(future_value || "0")
  @pay_at_beginning = pay_at_beginning || false
  @result           = solve(@rate, @num_periods, @present_value, @future_value, @pay_at_beginning)
end

Public Instance Methods

inspect() click to toggle source
# File lib/financial_calculator/pmt.rb, line 56
def inspect
  "PMT(#{result})"
end
pays_at_beginning?() click to toggle source

@return [Boolean] Whether the payments are made at the beginning of each period @api public

# File lib/financial_calculator/pmt.rb, line 52
def pays_at_beginning?
  @pay_at_beginning
end

Private Instance Methods

solve(rate, nper, pv, fv, pay_at_beginning) click to toggle source
# File lib/financial_calculator/pmt.rb, line 62
def solve(rate, nper, pv, fv, pay_at_beginning)
  type = pay_at_beginning ? 1 : 0
  @result = ((pv * (1 + rate) ** nper) + fv) * rate / ((1 + rate * type) * (1 - (1 + rate) ** nper))
end