How to format a string with floats in Ruby using #{variable}?

Solution 1:

You can use "#{'%.2f' % var}":

irb(main):048:0> num = 3.1415
=> 3.1415
irb(main):049:0> "Pi is: #{'%.2f' % num}"
=> "Pi is: 3.14"

Solution 2:

Use round:

"Current amount: #{amount.round(2)}"