Calculate percentage in Rails/Ruby

Hi,

i have unique visitors and transactions in my database, wich are showed through an swf-graph. This works fine.

Now i want to calculate the conversion-ratio in percentage:

visitors/transactions*100= ratio in percentage

Is there a function in ruby/rails to realize that?

Grtz..remco

what do want more than just doing that calculation ? The number_to_percentage helper will format it nicely if that's what you want.

Fred

I don't know if there is a function in ruby or rails to do that, but I'm quite sure something like

ratio = visitors / transaction * 100

will do the trick.

p.s. : learn ruby, and a little of programming worth the effort.

Frederick Cheung wrote:

Is there a function in ruby/rails to realize that?

what do want more than just doing that calculation ? The number_to_percentage helper will format it nicely if that's what you want.

Fred

Hi,

i have 2 array's

t.set_data([46,28,33,48,54,49,34,40,36,43,56,55,57,49,38,35,36,62,44,59,40,55,27,37,47,56]) v.set_data([2442,1754,2887,3378,2937,2595,2871,2508,1874, 2699,3203,3044,3034,2558,3565,2299,2917,3686,3167,2824,3308,2731,2034,2888,3526, 3434])

The calculation that i want to perform is

t.set_data/v.setdata * 100 = percentage

remco

I'm guessing something like this:

>> x = [1,2,3] => [1, 2, 3] >> y = [5,6,7] => [5, 6, 7] >> combined = x.zip y => [[1, 5], [2, 6], [3, 7]] >> combined.each do |a,b| ?> puts a.to_f/b.to_f * 100 >> end 20.0 33.3333333333333 42.8571428571429

or once you have combined, you could easily do:

percentage = combined.map{|a,b| a.to_f/b.to_f*100}

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #4 parts a and b now available! http://sensei.zenunit.com/

Remco Swoany wrote:

Hi,

i have 2 array's

t.set_data([46,28,33,48,54,49,34,40,36,43,56,55,57,49,38,35,36,62,44,59,40,55,27,37,47,56]) v.set_data([2442,1754,2887,3378,2937,2595,2871,2508,1874, 2699,3203,3044,3034,2558,3565,2299,2917,3686,3167,2824,3308,2731,2034,2888,3526, 3434])

The calculation that i want to perform is

t.set_data/v.setdata * 100 = percentage

remco

you can try:

t.each_with_index do |i,j|   percentage = i/v[j] * 100 end

if they're combined ;D