Chris Dmello
Chris Dmello

Reputation: 13

'+' String can't be coerced into Fixnum (TypeError)

this is a simple calculation formula im doing trying ruby for the first time and stuck on a simple error bt need some guidance

#input
loan_amount = 0
rate = 0
period = 0

#output
monthly_payment = 0

print "Enter loan_amount: "
loan_amount = gets.to_f
print "Enter rate: "
rate = gets.to_f
print "Enter period: "
period = gets.to_f

Error when i calculate the formula I end up getting this [undefined method rate for main:Object (NoMethodError)]

monthly_payment = loan_amount((rate(1 + rate)**period)/(1 + rate)**period - 1)

puts "#{monthly_payment}"

Upvotes: -2

Views: 373

Answers (2)

rizidoro
rizidoro

Reputation: 13428

There is a gem called Exonio: https://github.com/Noverde/exonio . This gem implements some of the Excel financials methods like: PMT, IPMT, PV, NPER...

Upvotes: 0

Alexander Shlenchack
Alexander Shlenchack

Reputation: 3869

Method gets always return string

loan_amount = gets
=> "1"

You should transform strings to numbers before calculation:

loan_amount = gets
loan_amount = loan_amount.to_f
...

Upvotes: 1

Related Questions