finance_rb 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52d806b7797622aa42c8a3d13197ddd3e66817dd41199751cc402d2e2ede4741
4
- data.tar.gz: 3404f5b73ededdd707d82f976a61bed2b575a3e00b448c1b056a06c92169bd5a
3
+ metadata.gz: 07e154f5ba5d444fe1e2c782c39a3d21a79b9fd3d413fba6e7c5285fa5f07df4
4
+ data.tar.gz: bef7aea3a7f54df1fc69d05619e9416637c717ce7e8b4d3a211573fbfbf5a6df
5
5
  SHA512:
6
- metadata.gz: b2114092617e4120fc0b4e61d5e98cd0c227c882605e10fb0bfafdeb761f8c6dc8124a82ac729b548f4365f1a7fa17b061ad14a1e8d1befb6608ced7f4d68634
7
- data.tar.gz: c42f87903359390b44fdb6472baacf24f6fc60934961cd16c007675e1adb231c0122bfca514c4ceffab7506f66b13b95736474c85ae23b4d4d103259f747f718
6
+ metadata.gz: bf67fafbda3defb85d73581d02b3dc00b26f83c6b03e4f35bd5c2d3a0ac661a823cd737d8055c54309b8a6d6feba8be7d03675b39ddb95b2e8f7ceaa94a7e472
7
+ data.tar.gz: 4f429be0368ac5e986c689fcd6afd146cec1676741d149ced828ae6d03e38e2d2e1ebdfeae88fce1d76de1a31c51a08fb0809577362f07f1de0e70e761bfa8b1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.2.2] - 2021-05-01
2
+
3
+ ### Added
4
+ * Implement `Finance::Loan#pv`
5
+
1
6
  ## [0.2.1] - 2021-05-01
2
7
 
3
8
  ### Added
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 | | Computes the present value of a payment|
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Finance
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finance_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vlad Dyachenko