user user - 5 months ago 8
Ruby Question

Rails round number

I have number

num = 24.89808
and want to round it to
24.89


How can i do it?

i tried
num.round(2)
but it gives me
24.9
also
number_to_currency=>24.90

Answer

If you are talking about rounding, 24.9 is by all means the correct result. Whether you are interested in ceiling it, here you go:

(24.89808 * 100).floor / 100.0
#⇒ "24.89"