From 3996a06369559416ca764af474f7b33a9d49d978 Mon Sep 17 00:00:00 2001 From: Gaurav Agarwal Date: Sat, 12 Oct 2019 13:52:48 +0530 Subject: [PATCH] Explaining compounding with a ruby script. --- .ruby-version | 1 + Gemfile | 7 +++++++ Gemfile.lock | 13 +++++++++++++ explain-compounding.rb | 15 +++++++++------ 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 .ruby-version create mode 100755 Gemfile create mode 100644 Gemfile.lock diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..ec1cf33 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.6.3 diff --git a/Gemfile b/Gemfile new file mode 100755 index 0000000..53c9a95 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +gem "humanize" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..72b0d3b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + humanize (2.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + humanize + +BUNDLED WITH + 2.0.2 diff --git a/explain-compounding.rb b/explain-compounding.rb index 6e809a3..4569563 100755 --- a/explain-compounding.rb +++ b/explain-compounding.rb @@ -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}?" @@ -12,7 +14,7 @@ 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 @@ -20,10 +22,11 @@ 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)}"