Skip to content

Commit 322d101

Browse files
authored
Merge pull request #16 from wowinter13/feat/loan_pv
Implement Finance::Loan#pv
2 parents 99ec8c8 + 3252cf7 commit 322d101

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ which are as follows:
1616
| pmt || Computes the fixed periodic payment(principal + interest) made against a loan amount|
1717
| ppmt || Computes principal payment for a loan|
1818
| nper || Computes the number of periodic payments|
19-
| pv | | Computes the present value of a payment|
19+
| pv | | Computes the present value of a payment|
2020
| rate | | Computes the rate of interest per period|
2121
| irr || Computes the internal rate of return|
2222
| npv || Computes the net present value of a series of cash flow|

lib/finance/loan.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Loan
2525

2626
# @return [Float] The number of periods to be compounded for. (I.e. Nper())
2727
# Defaults to 1.
28+
# You can use #nper method to calculate value if param is not defined.
2829
attr_accessor :duration
2930

3031
# @return [Float] Future value.
@@ -178,6 +179,29 @@ def fv(payment: nil)
178179
-((amount * factor) + (final_payment.to_f * second_factor))
179180
end
180181

182+
# Pv computes present value.
183+
# Required Loan arguments: nominal_rate, duration, payment, future_value, *ptype
184+
#
185+
# @return [Float] The present value.
186+
#
187+
# @example
188+
# require 'finance_rb'
189+
# Finance::Loan.new(nominal_rate: 0.24, duration: 12, future_value: 1000, payment: -300, ptype: :ending).pv
190+
# #=> 2384.1091906935
191+
#
192+
# @see http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
193+
# @see [WRW] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May).
194+
# Open Document Format for Office Applications (OpenDocument)v1.2,
195+
# Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version,
196+
# Pre-Draft 12. Organization for the Advancement of Structured Information
197+
# Standards (OASIS). Billerica, MA, USA. [ODT Document].
198+
def pv
199+
factor = (1.0 + monthly_rate)**duration
200+
second_factor = (factor - 1) * (1 + monthly_rate * ptype) / monthly_rate
201+
202+
-(future_value + (payment.to_f * second_factor)) / factor
203+
end
204+
181205
private
182206

183207
def initialize_payment_type(ptype)

spec/finance/loan_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@
7171
end
7272
end
7373

74+
describe '#pv' do
75+
context 'when :ptype == beginning' do
76+
it 'calculates correct pv value' do
77+
loan = Finance::Loan.new(
78+
nominal_rate: 0.24, duration: 12, future_value: 1000, payment: -300, ptype: :beginning
79+
)
80+
expect(loan.pv).to eq(2447.5612380190028)
81+
end
82+
end
83+
84+
context 'when :ptype == ending' do
85+
it 'calculates correct pv value' do
86+
loan = Finance::Loan.new(
87+
nominal_rate: 0.24, duration: 12, future_value: 1000, payment: -300, ptype: :ending
88+
)
89+
expect(loan.pv).to eq(2384.1091906935)
90+
end
91+
end
92+
end
93+
7494
describe '#ipmt' do
7595
context 'when 1 period' do
7696
it 'calculates correct ipmt value' do

0 commit comments

Comments
 (0)