Class: FinCalc

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

Overview

FinCalc is a class with methods to perform basic financial operations It works inspired in the HP-12 financial calclulator. Feel free to add more financial operations as needed Author: Hernan Velasquez

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFinCalc

Class constructor. Clear the calculator



18
19
20
# File 'lib/fincalc.rb', line 18

def initialize
  clear
end

Instance Attribute Details

#cfi=(value) ⇒ Object (writeonly)

Ammount of the i-esm paymanent in a no-constant cash flow



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

def cfi=(value)
  @cfi = value
end

#i=(value) ⇒ Object (writeonly)

Interest rate



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

def i=(value)
  @i = value
end

#n=(value) ⇒ Object (writeonly)

Number of periods



9
10
11
# File 'lib/fincalc.rb', line 9

def n=(value)
  @n = value
end

#periodCounterObject (readonly)

To be used to calculate the NPV of a cash flow. The internalo period counter



15
16
17
# File 'lib/fincalc.rb', line 15

def periodCounter
  @periodCounter
end

#pmt=(value) ⇒ Object (writeonly)

Ammount of a constant payment in a cash flow



11
12
13
# File 'lib/fincalc.rb', line 11

def pmt=(value)
  @pmt = value
end

Class Method Details

.calculatePerpetuityPV(pPaymentValue, pInterestRate) ⇒ Object

Static method to calculate the present value of a perpetuity taking the attribute’s values



55
56
57
58
# File 'lib/fincalc.rb', line 55

def self.calculatePerpetuityPV (pPaymentValue, pInterestRate)
  i = (pInterestRate / 100.0)
  return pPaymentValue / i
end

Instance Method Details

#calculateAnnuityObject

Calculate the present value of an annuity based on the attributes values already set



44
45
46
47
# File 'lib/fincalc.rb', line 44

def calculateAnnuity
  rate = @i /100.0
  @pmt*((1 - (1 / ((1 + rate)**@n))) / rate)
end

#calculatePerpetuityPVObject

Calculates the present value of a perpetuity taking the attribute’s values



50
51
52
# File 'lib/fincalc.rb', line 50

def calculatePerpetuityPV
  return @pmt / (@i / 100.0)
end

#cfiPeriodi(pValue) ⇒ Object

Set the cashflow value for period i



38
39
40
41
# File 'lib/fincalc.rb', line 38

def cfiPeriodi (pValue)
  @cfi[@periodCounter] = pValue
  @periodCounter = @periodCounter + 1
end

#cfPeriod0(pValue) ⇒ Object

Set the cashflow value for period 0



33
34
35
# File 'lib/fincalc.rb', line 33

def cfPeriod0 (pValue)
  @cfi[0] = pValue
end

#clearObject

Method to clear the calculator. Set all variables in their defaults



23
24
25
26
27
28
29
30
# File 'lib/fincalc.rb', line 23

def clear
  @i = 0
  @n = 0
  @pmt = 0
  @periodCounter = 1
  @cfi = Array.new
  @cfi[0] = 0
end

#NPVObject

Calculates the Net Present Value of a cashflow



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fincalc.rb', line 61

def NPV 
  npv = 0;
  rate = @i / 100.0;
  j = 0
  while j < @periodCounter
    val = @cfi[j]  / (1+rate)**j
    npv = npv + val
    j = j+1
  end
  return npv
end