Class: FinancialCalculator::Pv
- Inherits:
-
Object
- Object
- FinancialCalculator::Pv
- Includes:
- Validator
- Defined in:
- lib/financial_calculator/pv.rb
Overview
Calculate the present value of a series of equal of payments
Instance Attribute Summary collapse
-
#future_value ⇒ Numeric
readonly
The value remaining after the final payment has been made.
-
#num_periods ⇒ Numeric
readonly
The number of periodic payments.
-
#payment ⇒ Numeric
readonly
The amount of the periodic payment.
-
#rate ⇒ Numeric
readonly
The discount rate used in the calculation.
-
#result ⇒ Numeric
readonly
The result of the present value calculation.
Instance Method Summary collapse
-
#initialize(rate, num_periods, payment, future_value = 0, pay_at_beginning = false) ⇒ FinancialCalculator::Pv
constructor
Create a new present value calculation.
- #inspect ⇒ Object
-
#pays_at_beginning? ⇒ Boolean
Whether the payments are made at the beginning of each period.
Constructor Details
#initialize(rate, num_periods, payment, future_value = 0, pay_at_beginning = false) ⇒ FinancialCalculator::Pv
Create a new present value calculation
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/financial_calculator/pv.rb', line 34 def initialize(rate, num_periods, payment, future_value = 0, pay_at_beginning = false) validate_numerics(rate: rate, num_periods: num_periods, payment: payment, future_value: future_value) if num_periods < 0 raise ArgumentError.new('Cannot calculate present value with negative periods. Use future value instead.') end @rate = Flt::DecNum(rate.to_s) @num_periods = Flt::DecNum(num_periods.to_s) @payment = Flt::DecNum(payment.to_s) @future_value = Flt::DecNum(future_value.to_s) @pay_at_beginning = pay_at_beginning @result = solve(@rate, @num_periods, @payment, @future_value, pay_at_beginning) end |
Instance Attribute Details
#future_value ⇒ Numeric (readonly)
Returns The value remaining after the final payment has been made.
16 17 18 |
# File 'lib/financial_calculator/pv.rb', line 16 def future_value @future_value end |
#num_periods ⇒ Numeric (readonly)
Returns The number of periodic payments.
10 11 12 |
# File 'lib/financial_calculator/pv.rb', line 10 def num_periods @num_periods end |
#payment ⇒ Numeric (readonly)
Returns The amount of the periodic payment.
13 14 15 |
# File 'lib/financial_calculator/pv.rb', line 13 def payment @payment end |
#rate ⇒ Numeric (readonly)
Returns The discount rate used in the calculation.
7 8 9 |
# File 'lib/financial_calculator/pv.rb', line 7 def rate @rate end |
#result ⇒ Numeric (readonly)
Returns The result of the present value calculation.
19 20 21 |
# File 'lib/financial_calculator/pv.rb', line 19 def result @result end |
Instance Method Details
#inspect ⇒ Object
49 50 51 |
# File 'lib/financial_calculator/pv.rb', line 49 def inspect "PV(#{result})" end |
#pays_at_beginning? ⇒ Boolean
Returns Whether the payments are made at the beginning of each period.
54 55 56 |
# File 'lib/financial_calculator/pv.rb', line 54 def pays_at_beginning? @pay_at_beginning end |