New to programming help with this study drill

Been programming only 3 days. Sorry for the extreme newbie question but
I am working on a study drill where I am suppose to ask the user for a
money amount and give back 10% of it. So far It looks like it works but
I am having problems when printing out the result. This is what I have

print “Return amount set at 10%\n”

prompt user for total amount

print "Enter total amount: "
amount = gets.chomp.to_f

amt_return = amount * 0.10

puts “Your return is: #{amt_return}”

When I type in for example 103.4 the return is 10.3400000002

In the exercises I haven’t got to the point where it teaches this so
I was wondering how can I get it to only show 10.34 and not all the
zeros after it?

Thank you

I went ahead and added the round(2) after doing some research and now it
is returning 10.34 which is what I wanted to see. Is this the easiest
way to achieve this? I am open to seeing other ways if there is or if
this isn’t a good way to do it

puts "Your return is: #{amt_return.round(2)}

If you need only 2 decimal digits, rounding is the easiest way to
achieve this.

Note that it shows 10.3400000002 and not exactly 10.34 because that is
how floating point numbers work. See here for details:

If accuracy is critical and you can not have any possible errors
introduced by floats, you could…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs