Class: TimeValue

Inherits:
Object
  • Object
show all
Defined in:
lib/timevalue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = 0, i = 0, pv = 0.0, pmt = 0.0, fv = 0.0) ⇒ TimeValue

Returns a new instance of TimeValue.



5
6
7
8
9
10
11
# File 'lib/timevalue.rb', line 5

def initialize(n = 0, i = 0, pv = 0.0, pmt = 0.0, fv = 0.0)
    @n = n
    @i = i.to_f
    @pv = pv.to_f
    @pmt = pmt.to_f
    @fv = fv.to_f
end

Instance Attribute Details

#fvObject

Returns the value of attribute fv.



3
4
5
# File 'lib/timevalue.rb', line 3

def fv
  @fv
end

#iObject

Returns the value of attribute i.



3
4
5
# File 'lib/timevalue.rb', line 3

def i
  @i
end

#nObject

Returns the value of attribute n.



3
4
5
# File 'lib/timevalue.rb', line 3

def n
  @n
end

#pmtObject

Returns the value of attribute pmt.



3
4
5
# File 'lib/timevalue.rb', line 3

def pmt
  @pmt
end

#pvObject

Returns the value of attribute pv.



3
4
5
# File 'lib/timevalue.rb', line 3

def pv
  @pv
end

Instance Method Details

#calc_fvObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/timevalue.rb', line 25

def calc_fv()
    i = @i / 100.0
    #Growth of initial contribution
    fvp = @pv * ((1 + i) ** @n)
    #Growth of payments
    fva = @pmt * (((1 + i) ** @n)-1) / i
    @fv = (fvp + fva) * (-1)
    #Round
    @fv = (@fv*100).round / 100.0
end

#calc_nObject



36
37
38
39
# File 'lib/timevalue.rb', line 36

def calc_n()
    i = @i / 100.0
    @n = (Math.log((@pmt - (i * @fv))/(@pmt + (i * @pv)))/Math.log(1 + i)).round(0)
end

#calc_pmtObject



41
42
43
44
45
# File 'lib/timevalue.rb', line 41

def calc_pmt()
    i = @i / 100.0
    @pmt = (-i*(@fv+(@pv*((1+i)**n))))/(((1+i)**n)-1)
    @pmt = (@pmt*100).round / 100.0
end

#calc_pvObject

Base formula, ordinary annuity: -PV = [FV/((1+i)^n)] + (PMT/i)*



14
15
16
17
18
19
20
21
22
23
# File 'lib/timevalue.rb', line 14

def calc_pv()
    i = @i / 100.0
    #Initial contribution
    pvf = @fv / ((1 + i) ** @n)
    #Present value of annuity
    pva = (@pmt/i) * (1-(1/((1+i)**@n)))
    @pv = (pvf + pva) * (-1)
    #Round
    @pv = (@pv*100).round / 100.0
end