class FinancialCalculator::Nper

Calculates the number of periods in an annuity

Attributes

future_value[R]

@return [Numeric] The ending value of the annuity. Defaults to 0 @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

payment[R]

@return [Numeric] The amount of each payment made @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 number of payments @api public

result[R]

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

Public Class Methods

new(rate, payment, 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] payment The amount of each payment 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::Nper] An instance of NPER calculation

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

  @rate             = Flt::DecNum(rate.to_s)
  @payment          = Flt::DecNum(payment.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, @payment, @present_value, @future_value, @pay_at_beginning)
end

Public Instance Methods

inspect() click to toggle source
# File lib/financial_calculator/nper.rb, line 56
def inspect
  "NPER(#{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/nper.rb, line 52
def pays_at_beginning?
  @pay_at_beginning
end

Private Instance Methods

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

  # TODO: Insert better error handling if either argument is negative.
  @result = Math.log((initial - fv * rate) / (initial + pv * rate)) / Math.log(1.0 + rate)
end