# Ruby can't do arithmetic? How do I fix this?

**URL:** <https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701>\
**Category:** Rails\
**Created:** [December 3, 2008, 3:05am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701 "2008-12-03T03:05:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [December 3, 2008, 3:05am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701/1 "2008-12-03T03:05:10Z")

</div>

Hi,

I have this code @accept\_ratio = @no\_quotes / @no\_accepted\_quotes. I  
am deviding an integer by an integer but the resultant is not an  
iterger it should be floating to x decimal places. I was under the  
impression that ruby took care of this sort of thing.

@no\_quotes is 551  
@no\_accepted\_quotes is 119

I am getting 4 in @accept\_ratio which is wrong. It should be 4.63…  
to however many decimal places.

How do I persuade ruby not to chop the decimal off? I would like  
results to 2 decimal places.

Thanks in advance,

Paul Thompson

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [December 3, 2008, 3:08am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701/2 "2008-12-03T03:08:41Z")

</div>

Paul Jonathan T. wrote:

> I am getting 4 in @accept\_ratio which is wrong. It should be 4.63…  
> to however many decimal places.
> 
> How do I persuade ruby not to chop the decimal off? I would like  
> results to 2 decimal places.
> 
> Thanks in advance,
> 
> Paul Thompson

Change one of them to float. Ruby uses integer arithmetic with  
integers.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [December 3, 2008, 3:21am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701/3 "2008-12-03T03:21:36Z")

</div>

Hi Norm,

I am not sure how to do that. I have tried .to\_f but that did not make  
any difference.

Paul

2008/12/3 Norm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [December 3, 2008, 3:36am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701/4 "2008-12-03T03:36:16Z")

</div>

Paul Jonathan T. wrote:

> Hi Norm,
> 
> I am not sure how to do that. I have tried .to\_f but that did not make  
> any difference.
> 
> Paul

When you say you tried .to\_f do you mean you tried

@accept\_ratio = @no\_quotes.to\_f / @no\_accepted\_quotes.to\_f

It works for me. You only really need to convert either of the  
arguments.

@accept\_ratio.to\_f = @no\_quotes / @no\_accepted\_quotes will not work  
because it will first evaluate to the integer 4 and then convert that to  
a float…

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [December 3, 2008, 3:55am UTC](https://www.ruby-forum.com/t/ruby-cant-do-arithmetic-how-do-i-fix-this/154701/5 "2008-12-03T03:55:35Z")

</div>

Thanks Norm,

Your second example is what I did (@accept\_ratio.to\_f = @no\_quotes /  
@no\_accepted\_quote) so it did not work.

but when I changed it to how you showed inyour first example, it  
worked just fine.

Thank you very much.

Paul

2008/12/3 Norm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
