Class: FinancialCalculator::Pv

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/financial_calculator/pv.rb

Overview

Calculate the future value of a series of equal of payments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate, num_periods, payment, future_value = 0, pay_at_beginning = false) ⇒ FinancialCalculator::Pv

Create a new present value calculation

Examples:

FinancialCalculator::Pv.new(0.02, 10, -100) #=> PresentValue(898.2585006242236)

Parameters:

  • rate (Numeric)

    The discount (interest) rate to use

  • num_periods (Numeric)

    The number of periodic payments

  • payment (Numeric)

    The amount of the periodic payment

  • future_value (Numeric) (defaults to: 0)

    The value remaining after the final payment has been made

  • pay_at_beginning (Boolean) (defaults to: false)

    Whether the payments are made at the beginning or end of each period

Raises:

  • (ArgumentError)

    Raises an ArgumentError if num_periods is less than 0 or a required numeric field is given a non-numeric value

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/financial_calculator/pv.rb', line 34

def initialize(rate, num_periods, payment, future_value = 0, pay_at_beginning = false)
  validate_numerics(rate: rate, num_periods: num_periods, payment: payment, future_value: future_value)

  if num_periods < 0
    raise ArgumentError.new('Cannot calculate present value with negative periods. Use future value instead.')
  end

  @rate             = Flt::DecNum(rate.to_s)
  @num_periods      = Flt::DecNum(num_periods.to_s)
  @payment          = Flt::DecNum(payment.to_s)
  @future_value     = Flt::DecNum(future_value.to_s)
  @pay_at_beginning = pay_at_beginning
  @result           = solve(@rate, @num_periods, @payment, @future_value, pay_at_beginning)
end

Instance Attribute Details

#future_valueNumeric (readonly)

Returns The value remaining after the final payment has been made.

Returns:

  • (Numeric)

    The value remaining after the final payment has been made



16
17
18
# File 'lib/financial_calculator/pv.rb', line 16

def future_value
  @future_value
end

#num_periodsNumeric (readonly)

Returns The number of periodic payments.

Returns:

  • (Numeric)

    The number of periodic payments



10
11
12
# File 'lib/financial_calculator/pv.rb', line 10

def num_periods
  @num_periods
end

#paymentNumeric (readonly)

Returns The amount of the periodic payment.

Returns:

  • (Numeric)

    The amount of the periodic payment



13
14
15
# File 'lib/financial_calculator/pv.rb', line 13

def payment
  @payment
end

#rateNumeric (readonly)

Returns The discount rate used in the calculation.

Returns:

  • (Numeric)

    The discount rate used in the calculation



7
8
9
# File 'lib/financial_calculator/pv.rb', line 7

def rate
  @rate
end

#resultNumeric (readonly)

Returns The result of the present value calculation.

Returns:

  • (Numeric)

    The result of the present value calculation



19
20
21
# File 'lib/financial_calculator/pv.rb', line 19

def result
  @result
end

Instance Method Details

#inspectObject



49
50
51
# File 'lib/financial_calculator/pv.rb', line 49

def inspect
  "PV(#{result})"
end

#pays_at_beginning?Boolean

Returns Whether the payments are made at the beginning of each period.

Returns:

  • (Boolean)

    Whether the payments are made at the beginning of each period



54
55
56
# File 'lib/financial_calculator/pv.rb', line 54

def pays_at_beginning?
  @pay_at_beginning
end