Maths with in Rails

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

    @arrived = @worksheet.time_arrived_customer     @left = @worksheet.time_left_office

    @left_cust = @worksheet.time_left_customer

    @travel = ((@arrived - @left) * 2)     @worked = (@left_cust - @arrived)

    @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

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

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.

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