# Simple division problems... I know horrible!

**URL:** <https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987>\
**Category:** Rails\
**Created:** [October 11, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987 "2007-10-11T21:55:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![new\_rails-ny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@new\_rails-ny](https://www.ruby-forum.com/u/new_rails-ny)\
**Post date:** [October 11, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987/1 "2007-10-11T21:55:37Z")

</div>

Hey all,  
Thanks for your time here but I am having problems with something  
that should be so simple…

@campaigns = Campaign.find(:all, :conditions =\> [“end\_date \>= ?”,  
Date.today])  
@total\_running = Campaign.sum(:impressions, :conditions =\> ["end\_date

> = ?", Date.today])  
> for campaign in @campaigns  
> campaign\_days = campaign.end\_date - campaign.start\_date  
> days = days + campaign\_days  
> end  
> @today = @total\_running / days

So as you can see I am running two simple queries and returning  
variables to the view to be displayed. Unfortunately the last line of  
code here is not working and I am at a loss. When I display @today it  
is giving me the values of total\_running and days with a / in the  
middle as if these were just strings when actually I would like the  
value for total\_running to be divided by days.

Example of what is displayed -  
123456/51

what I would like displayed -  
2420

I have tried to change the / to a + and it did correctly add the  
values together and display the sum of the two values so I am at a  
loss as to why it will not divide the numbers.

Thanks for the assistance.

---

<div class="post-metadata">

**Author:** ![new\_rails-ny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@new\_rails-ny](https://www.ruby-forum.com/u/new_rails-ny)\
**Post date:** [October 11, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987/2 "2007-10-11T22:18:29Z")

</div>

@today = @total\_running.to\_i / days

On 10/11/07, new\_rails-ny [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> campaign_days = campaign.end_date - campaign.start_date
> 
> ```
> 
> Thanks for the assistance.
> 
> >

–  
[http://www.bdcsoftware.com](http://www.bdcsoftware.com)" - Automotive CRM

---

<div class="post-metadata">

**Author:** ![new\_rails-ny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@new\_rails-ny](https://www.ruby-forum.com/u/new_rails-ny)\
**Post date:** [October 11, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987/3 "2007-10-11T22:51:20Z")

</div>

That did it, but I had to change both variables to “to\_i” any reason  
why I would have to do this for division but not addition? That is  
what really through me for a loop.

Thanks again.

The other thing about this

---

<div class="post-metadata">

**Author:** ![new\_rails-ny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@new\_rails-ny](https://www.ruby-forum.com/u/new_rails-ny)\
**Post date:** [October 11, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/simple-division-problems-i-know-horrible/116987/4 "2007-10-11T23:02:27Z")

</div>

> That did it, but I had to change both variables to “to\_i” any reason  
> why I would have to do this for division but not addition? That is  
> what really through me for a loop.

irb(main):001:0\> “12” + “34”  
=\> “1234”  
irb(main):002:0\> “12” / “34”  
NoMethodError: undefined method `/’ for “12”:String  
from (irb):2  
irb(main):004:0\> “12”.to\_i + “34”.to\_i  
=\> 46

There is a + method for strings. But not a ‘/’ method. So while it  
worked, odds are it wasn’t what you wanted…
