Re: How do I round off a float to x decimal places

Might be a little late but here’s my method

round only works on integers so have to

multiply the float to by 10000 to round it to 2 decimal places

and then correct the position of the decimal by

dividing by 100.0

def round(float, num_of_decimal_places)
exponent = num_of_decimal_places + 2
@float = float*(10exponent)
@float = @float.round
@float = @float / (10.0
exponent)
end

Gem

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs