This is my code trying to calculate the coumpoung interest
def coumpoundinterest
print "Enter the current balance: "
current_balance = gets
print "Enter the interest rate: "
interest_rate = gets
_year = 2012
i = 0
while i < 5 do
current_balance = current_balance + current_balance * interest_rate
puts "The balance in year " + (_year + i).to_s + " is $" + current_balance.to_s
i = i + 1
end
end
This is the line where I’m getting all the troubles
current_balance = current_balance + current_balance * interest_rate
If I keep the code the way it is, I get an error that string cannot be forced into FixNum. If I add .to_i after interest_rate, then I get the line multiply several times. How can I deal with arithmetics in ruby?
getswill return a string with\n. Yourcurrent_balanceandinterest_ratevariables are strings like"11\n""0.05\n". So if you only useinterest_rate.to_i. The operator*between a string and a fixnum will repeat the string several times according to the fixnum. Try to convert them into float both.