Class: FinancialCalculator::Fv

Inherits:
Object
  • Object
show all
Includes:
Validator
Defined in:
lib/financial_calculator/fv.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, present_value = 0, pay_at_beginning = false) ⇒ FinancialCalculator::Fv

Create a new future value calculation

Examples:

FinancialCalculator::Fv.new(0.02, 10, -100) #=> FV(1094.972100)

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

  • present_value (Numeric) (defaults to: 0)

    The current value

  • 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/fv.rb', line 34

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

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

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

Instance Attribute Details

#num_periodsNumeric (readonly)

Returns The number of periodic payments.

Returns:

  • (Numeric)

    The number of periodic payments



10
11
12
# File 'lib/financial_calculator/fv.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/fv.rb', line 13

def payment
  @payment
end

#present_valueNumeric (readonly)

Returns The current value.

Returns:



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

def present_value
  @present_value
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/fv.rb', line 7

def rate
  @rate
end

#resultNumeric (readonly)

Returns The result of the future value calculation.

Returns:

  • (Numeric)

    The result of the future value calculation



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

def result
  @result
end

Instance Method Details

#inspectObject



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

def inspect
  "FV(#{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/fv.rb', line 54

def pays_at_beginning?
  @pay_at_beginning
end