finance_rb 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/finance/loan.rb +24 -0
- data/lib/finance/version.rb +1 -1
- data/spec/finance/loan_spec.rb +20 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07e154f5ba5d444fe1e2c782c39a3d21a79b9fd3d413fba6e7c5285fa5f07df4
|
4
|
+
data.tar.gz: bef7aea3a7f54df1fc69d05619e9416637c717ce7e8b4d3a211573fbfbf5a6df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf67fafbda3defb85d73581d02b3dc00b26f83c6b03e4f35bd5c2d3a0ac661a823cd737d8055c54309b8a6d6feba8be7d03675b39ddb95b2e8f7ceaa94a7e472
|
7
|
+
data.tar.gz: 4f429be0368ac5e986c689fcd6afd146cec1676741d149ced828ae6d03e38e2d2e1ebdfeae88fce1d76de1a31c51a08fb0809577362f07f1de0e70e761bfa8b1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -16,7 +16,7 @@ which are as follows:
|
|
16
16
|
| pmt | ✅ | Computes the fixed periodic payment(principal + interest) made against a loan amount|
|
17
17
|
| ppmt | ✅ | Computes principal payment for a loan|
|
18
18
|
| nper | ✅ | Computes the number of periodic payments|
|
19
|
-
| pv |
|
19
|
+
| pv | ✅ | Computes the present value of a payment|
|
20
20
|
| rate | | Computes the rate of interest per period|
|
21
21
|
| irr | ✅ | Computes the internal rate of return|
|
22
22
|
| npv | ✅ | Computes the net present value of a series of cash flow|
|
data/lib/finance/loan.rb
CHANGED
@@ -25,6 +25,7 @@ module Finance
|
|
25
25
|
|
26
26
|
# @return [Float] The number of periods to be compounded for. (I.e. Nper())
|
27
27
|
# Defaults to 1.
|
28
|
+
# You can use #nper method to calculate value if param is not defined.
|
28
29
|
attr_accessor :duration
|
29
30
|
|
30
31
|
# @return [Float] Future value.
|
@@ -178,6 +179,29 @@ module Finance
|
|
178
179
|
-((amount * factor) + (final_payment.to_f * second_factor))
|
179
180
|
end
|
180
181
|
|
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
|
+
|
181
205
|
private
|
182
206
|
|
183
207
|
def initialize_payment_type(ptype)
|
data/lib/finance/version.rb
CHANGED
data/spec/finance/loan_spec.rb
CHANGED
@@ -71,6 +71,26 @@ RSpec.describe Finance::Loan do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
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
|
+
|
74
94
|
describe '#ipmt' do
|
75
95
|
context 'when 1 period' do
|
76
96
|
it 'calculates correct ipmt value' do
|