Ruby round to the 2nd decimal?

advertisements

Learning ruby and want to make a simple string where:

puts "A Honda Fit cost $#{3.98 * 10.6} to fill up."

The answer I'm getting is $42.1879999999995; would like the answer to round to two decimal places.

Thanks for the help.


You can also use the Float#round method

puts "A Honda Fit cost $#{(3.98 * 10.6).round(2)} to fill up."
#=> A Honda Fit cost $42.19 to fill up.