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

**URL:** <https://www.ruby-forum.com/t/re-how-do-i-round-off-a-float-to-x-decimal-places/72295>\
**Category:** Ruby\
**Created:** [October 16, 2006, 9:51am UTC](https://www.ruby-forum.com/t/re-how-do-i-round-off-a-float-to-x-decimal-places/72295 "2006-10-16T09:51:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [October 16, 2006, 9:51am UTC](https://www.ruby-forum.com/t/re-how-do-i-round-off-a-float-to-x-decimal-places/72295/1 "2006-10-16T09:51:24Z")

</div>

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\*(10**exponent)  
 @float = @float.round  
 @float = @float / (10.0**exponent)  
end

Gem
