# Maths with in Rails

**URL:** <https://discuss.rubyonrails.org/t/maths-with-in-rails/60788>\
**Category:** rubyonrails-talk\
**Created:** [September 8, 2011, 8:37am UTC](https://discuss.rubyonrails.org/t/maths-with-in-rails/60788 "2011-09-08T08:37:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene\_de\_Winnaar](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@Eugene\_de\_Winnaar](https://discuss.rubyonrails.org/u/Eugene_de_Winnaar)\
**Post date:** [September 8, 2011, 8:37am UTC](https://discuss.rubyonrails.org/t/maths-with-in-rails/60788/1 "2011-09-08T08:37:09Z")

</div>

Hi All

I have a idea that I am doing and it is working so far but when it comes to displaying the data that I am getting with the maths that I am doing I am not getting the same result.

This is what I have

&nbsp;&nbsp;&nbsp;&nbsp;@arrived = @worksheet.time\_arrived\_customer &nbsp;&nbsp;&nbsp;&nbsp;@left = @worksheet.time\_left\_office

&nbsp;&nbsp;&nbsp;&nbsp;@left\_cust = @worksheet.time\_left\_customer

&nbsp;&nbsp;&nbsp;&nbsp;@travel = ((@arrived - @left) \* 2) &nbsp;&nbsp;&nbsp;&nbsp;@worked = (@left\_cust - @arrived)

&nbsp;&nbsp;&nbsp;&nbsp;@total = (@travel + @worked) / 60

So what I did was in the DB I am saving the hours in their minute form.

08:00 = 480 min 08:15 = 495 min

And so on.

I am 100% sure the maths is working as I firstly did it on paper and took those steps and put them into the action.

When it comes to the display I am superpose to get 2.75 but I only see 2 why is that

I have looked at number\_with\_precision but rather then getting 2.75 I get 2.00

Any Ideas would be appreciated.

Regards

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 8, 2011, 1:30pm UTC](https://discuss.rubyonrails.org/t/maths-with-in-rails/60788/2 "2011-09-08T13:30:52Z")

</div>

> I am 100% sure the maths is working as I firstly did it on paper and took those steps and put them into the action.
> 
> When it comes to the display I am superpose to get 2.75 but I only see 2 why is that

by default if you are working with integers you'll always get integers back (by truncation). If you want floating point numbers then you should convert the numbers you're working with to floating point numbers first (using to\_f), so

5 /2 will always return 2, but 5.0 / 2 will produce 2.5

> I have looked at number\_with\_precision but rather then getting 2.75 I get 2.00

number\_with\_precision is for formatting a number, you still need to actually have a number with the floating point component.

Fred

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [September 9, 2011, 12:06pm UTC](https://discuss.rubyonrails.org/t/maths-with-in-rails/60788/3 "2011-09-09T12:06:35Z")

</div>

Without the code used in the display action it's difficult to know what's going on but I'd guess you're using an integer at some point and that causes loss of precision.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 9, 2011, 12:51pm UTC](https://discuss.rubyonrails.org/t/maths-with-in-rails/60788/4 "2011-09-09T12:51:35Z")

</div>

Frederick Cheung wrote in post #1020782:

> by default if you are working with integers you'll always get integers back (by truncation). If you want floating point numbers then you should convert the numbers you're working with to floating point numbers first (using to\_f), so
> 
> 5 /2 will always return 2, but 5.0 / 2 will produce 2.5

This might also help to clarify what's happening:

$ irb ruby-1.9.2-p290 :014 \> x = 5 =\> 5 ruby-1.9.2-p290 :015 \> y = 3 =\> 3 ruby-1.9.2-p290 :016 \> z = x / y =\> 1 ruby-1.9.2-p290 :017 \> x.class =\> Fixnum ruby-1.9.2-p290 :018 \> y.class =\> Fixnum ruby-1.9.2-p290 :019 \> z.class =\> Fixnum ruby-1.9.2-p290 :020 \> x = 5 =\> 5 ruby-1.9.2-p290 :021 \> y = 3.0 =\> 3.0 ruby-1.9.2-p290 :022 \> z = x / y =\> 1.6666666666666667 ruby-1.9.2-p290 :023 \> x.class =\> Fixnum ruby-1.9.2-p290 :024 \> y.class =\> Float ruby-1.9.2-p290 :025 \> z.class =\> Float ruby-1.9.2-p290 :026 \> x = 5 =\> 5 ruby-1.9.2-p290 :027 \> y = 3 =\> 3 ruby-1.9.2-p290 :028 \> z = x.fdiv(y) =\> 1.6666666666666667 ruby-1.9.2-p290 :029 \> x.class =\> Fixnum ruby-1.9.2-p290 :030 \> y.class =\> Fixnum ruby-1.9.2-p290 :031 \> z.class =\> Float ruby-1.9.2-p290 :032 \> z = x.quo(y) =\> (5/3) ruby-1.9.2-p290 :033 \> z.class =\> Rational ruby-1.9.2-p290 :034 \> z.to\_f =\> 1.6666666666666667 ruby-1.9.2-p290 :035 \> z.to\_i =\> 1
