Skip to content

Instantly share code, notes, and snippets.

@brandoncc
Created May 8, 2015 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandoncc/bfd3eb016fd66627c260 to your computer and use it in GitHub Desktop.
Save brandoncc/bfd3eb016fd66627c260 to your computer and use it in GitHub Desktop.
def balance_to_dollars(balance)
balance = "#{'%.2f' % balance}"
dollars = balance.split('.')[0]
cents = balance.split('.')[1]
dollars = dollars.reverse.gsub(/...(?=.)/,'\&,').reverse
"$#{dollars}.#{cents}"
end
print "What is your current age? "
start_age = gets.chomp.to_i
print "What age would you like to calculate through? "
goal_age = gets.chomp.to_i
print "What is the current balance of the account? "
current_balance = gets.chomp.to_i
print "How much are you going to deposit each year? "
yearly_deposit = gets.chomp.to_i
print "What yield percentage would you like to calculate at? "
yield_percentage = gets.chomp.to_i / 100.0
(goal_age - start_age + 1).times do |i|
current_age = start_age + i
if i == 0
puts "You started at age #{start_age} with #{balance_to_dollars(current_balance)}."
else
current_balance += yearly_deposit
current_balance *= (1 + yield_percentage)
puts "At age #{current_age} you will have #{balance_to_dollars(current_balance)}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment