# I have some Ruby math questions that need answering

**URL:** <https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230>\
**Category:** Ruby\
**Created:** [July 17, 2009, 5:55am UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230 "2009-07-17T05:55:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 5:55am UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230/1 "2009-07-17T05:55:50Z")

</div>

When handling percentage calculations:

Simple 99/100…

Will I always have to do 99.to\_f/100.to\_f in order to receive 0.99? Or  
is there a simpler way of performing this task in ruby…

I notice that there doesn’t appear to be a round method with decimal  
places (or perhaps I can’t find one) so I found this one which appears  
to work:

class Float  
def round\_to(x)  
(self \* 10**x).round.to\_f / 10**x  
end

def ceil\_to(x)  
(self \* 10**x).ceil.to\_f / 10**x  
end

def floor\_to(x)  
(self \* 10**x).floor.to\_f / 10**x  
end  
end

The question I have is can I expand upon divisor behavior and create  
something that changes the way ruby behaves with integers/floats on a  
more permanent basis?

I only ask because I’m working with standard deviation and some advanced  
mathematics that revolve around calculating variance. I just don’t want  
to experience any gotchas halfway down my project.

Lastly, is there a good core site that houses all of the API for ruby  
math functions or refined methods that deal with variance?

Many thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 6:06am UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230/2 "2009-07-17T06:06:58Z")

</div>

On Jul 16, 2009, at 11:55 PM, Älphä Blüë wrote:

> When handling percentage calculations:
> 
> Simple 99/100…
> 
> Will I always have to do 99.to\_f/100.to\_f in order to receive 0.99?  
> Or  
> is there a simpler way of performing this task in ruby…

99.0/100 is sufficient here

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 7:21am UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230/3 "2009-07-17T07:21:10Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> When handling percentage calculations:
> 
> Simple 99/100…
> 
> Will I always have to do 99.to\_f/100.to\_f in order to receive 0.99? Or  
> is there a simpler way of performing this task in ruby…

or you use

require ‘mathn’

and I bet, there is much stuff in the facets-pakage:  
[http://facets.rubyforge.org/](http://facets.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 7:30am UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230/4 "2009-07-17T07:30:32Z")

</div>

99.0/100 or 99/100.0 will also work! You would have to keep one floating  
point to get a floating point result!

regards,  
Vikas Sarin  
Senior Software Quality Engineer  
QA Infotech  
Email: removed\_email\_address@domain.invalid  
Mob#: +919810319641

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 17, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/i-have-some-ruby-math-questions-that-need-answering/172230/5 "2009-07-17T15:27:38Z")

</div>

Thanks everyone - I appreciate the information. This does help me out  
going forward. I’ll look into facets as well.
