Wednesday, December 24, 2008

To resolve problem of rounding-off numbers

Following code is used to rounding-off any floating numbers with specified decimal places. There is also helper method provided by rails to do the same which is "number_with_precision". But if we want to rounding-off the floating number in controller, then following one-liner code may be helpful for that case.

# Specify your number to be round-off instead of FLOATING_NUMBER and how many decimal places you want instead of i.

(FLOATING_NUMBER * 10**i).round.to_f / 10**i

No comments:

Post a Comment