Skip to content

Commit

Permalink
Explaining compounding with a ruby script.
Browse files Browse the repository at this point in the history
  • Loading branch information
algogrit committed Oct 12, 2019
1 parent 7aa405e commit 3996a06
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.6.3
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "humanize"
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
humanize (2.2.1)

PLATFORMS
ruby

DEPENDENCIES
humanize

BUNDLED WITH
2.0.2
15 changes: 9 additions & 6 deletions explain-compounding.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env ruby

initial_amount = 100000
rate = 7
require 'humanize'

initial_amount = 1_26_000
rate = 9.0
years = 10

puts "Use defaults - Amount: #{initial_amount}, Rate: #{rate}, Years: #{years}?"
Expand All @@ -12,18 +14,19 @@
initial_amount = gets.to_i

puts "Enter rate of interest: "
rate = gets.to_i
rate = gets.to_f

puts "Enter total years: "
years = gets.to_i
end

amount = initial_amount

puts "You invested #{amount}"
puts "You invested \"#{amount.humanize}\" for #{years} years at #{rate}% compounded"

years.times do |i|
amount = amount * ( 1 + (rate / 100.0) )
puts "At end of #{i+1} year: #{amount}"
amount = (amount * ( 1 + (rate / 100.0) )).round(2)
puts "At end of #{i+1} year: #{amount} or \"#{amount.humanize}\""
end

puts "If you invest it for another #{years} years: #{amount * (( 1 + (rate / 100.0) ) ** years)}"

0 comments on commit 3996a06

Please sign in to comment.