You could make it generic so that you don’t repeat yourself everywhere
you need to round to decimal places.
I have this in my app:
class Float
alias :round_off :round
def round( d=0.0 )
rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d)
rounded = rounded.to_i if d == 0.0
rounded
end
end
class Float
alias :round_off :round
def round( d=0.0 )
rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d)
rounded = rounded.to_i if d == 0.0
rounded
end
end
Rails already provides a helper, number_to_currency, described here:
class Float
alias :round_off :round
def round( d=0.0 )
rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d)
rounded = rounded.to_i if d == 0.0
rounded
end
end
Rails already provides a helper, number_to_currency, described here: