# Slightly OT - Ruby division

**URL:** <https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397>\
**Category:** Rails\
**Created:** [December 15, 2005, 11:28pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397 "2005-12-15T23:28:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:28pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/1 "2005-12-15T23:28:02Z")

</div>

Hey all,

In my code, it seems that when I divide two integers, the result is an  
integer. Is there any way to make it such that when I divide two  
integers,  
the result is a double?

Any help appreciated,

Jin

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:33pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/2 "2005-12-15T23:33:05Z")

</div>

Jin L. wrote:

> Hey all,
> 
> In my code, it seems that when I divide two integers, the result is an  
> integer. Is there any way to make it such that when I divide two  
> integers,  
> the result is a double?

No, you have to convert one of them to a float first:  
x.to\_f/y

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/3 "2005-12-15T23:34:04Z")

</div>

int1 / int2.to\_f

this will force it into a float.

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/4 "2005-12-15T23:34:05Z")

</div>

On 16/12/2005, at 11:27 AM, Jin L. wrote:

> In my code, it seems that when I divide two integers, the result is  
> an integer. Is there any way to make it such that when I divide two  
> integers, the result is a double?

Ruby never automatically casts (except between Fixnum and Bignum), so  
you have to cast to float using Fixnum#to\_f

1.to\_f / 5.to\_f # =\> 0.2

–  
Phillip H.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:37pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/5 "2005-12-15T23:37:07Z")

</div>

On 12/15/05, Jin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey all,
> 
> In my code, it seems that when I divide two integers, the result is an  
> integer. Is there any way to make it such that when I divide two integers,  
> the result is a double?

a = 10  
b = 3  
a/b.to\_f

-Matt

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:40pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/6 "2005-12-15T23:40:17Z")

</div>

On 12/16/05, Jin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey all,
> 
> In my code, it seems that when I divide two integers, the result is an  
> integer. Is there any way to make it such that when I divide two integers,  
> the result is a double?
> 
> Any help appreciated,

irb(main):001:0\> 5/3  
=\> 1  
irb(main):002:0\> 5.to\_f/3  
=\> 1.66666666666667  
irb(main):003:0\> 5\*1.0/3  
=\> 1.66666666666667

martin

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 15, 2005, 11:37pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/7 "2005-12-15T23:37:06Z")

</div>

This might help:

> > a = 1  
> > =\> 1
> 
> > b = 2  
> > =\> 2
> 
> > a / b  
> > =\> 0
> 
> > a.to\_f / b  
> > =\> 0.5
> 
> > a / b.to\_f  
> > =\> 0.5
> 
> > a.to\_f / b.to\_f  
> > =\> 0.5

Regards,  
Douglas

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 16, 2005, 5:54pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/8 "2005-12-16T17:54:44Z")

</div>

Jin L. \<jinslee@…\> writes:

> Hey all,  
> In my code, it seems that when I divide two integers, the result is an  
> integer. Is there any way to make it such that when I divide two  
> integers, the result is a double?  
> Any help appreciated,  
> Jin

Apparently Float is not only a class, but also a method! I gather, then,  
that  
Ruby is sort of Lisp-2 (non-methods and methods have different  
namespaces).

#\> Float(3)  
3.0  
#\> Float(22)/7  
3.14285714285714

---

<div class="post-metadata">

**Author:** ![typej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@typej](https://www.ruby-forum.com/u/typej)\
**Post date:** [December 16, 2005, 6:00pm UTC](https://www.ruby-forum.com/t/slightly-ot-ruby-division/50397/9 "2005-12-16T18:00:47Z")

</div>

> Apparently Float is not only a class, but also a method! I gather, then, that  
> Ruby is sort of Lisp-2 (non-methods and methods have different namespaces).
> 
> #\> Float(3)  
> 3.0  
> #\> Float(22)/7  
> 3.14285714285714

#to\_f is usually used instead:

#\> 3.to\_f  
3.0  
#\> 22.to\_f / 7

> 3.14285714285714

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
