-5

I want to display a price such that if the answer is 10.1 the ouput would be 10.10 and if the answer is 20, the output would be 20.00

I have gone through the forums and used so many examples but it still hasn't worked for me. The answer is for rails but I am working on Ruby.s= '%2f' % @price and the output I get is 12.100000

Community
  • 1
  • 1
Kenigbolo
  • 183
  • 2
  • 9

2 Answers2

4

You have to add a dot in the format to say you want the 2 to be the precision after the decimal dot:

s = '%.2f' % @price
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
2

You could also use the round method, so:

12.100000.round(2) => 12.10
Jorge Lopez Jr
  • 257
  • 3
  • 8