Calculate percentage in Rails/Ruby

100 views
Skip to first unread message

Remco Swoany

unread,
Aug 27, 2008, 2:28:12 AM8/27/08
to rubyonra...@googlegroups.com
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
--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Aug 27, 2008, 2:43:28 AM8/27/08
to rubyonra...@googlegroups.com

On 27 Aug 2008, at 07:28, Remco Swoany wrote:

>
> 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?

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

Gabriel Laskar

unread,
Aug 27, 2008, 2:45:08 AM8/27/08
to rubyonra...@googlegroups.com
On Wed, Aug 27, 2008 at 8:28 AM, Remco Swoany
<rails-mai...@andreas-s.net> wrote:
>
> 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?
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.


--
Gabriel Laskar <bibi...@gmail.com>

Remco Swoany

unread,
Aug 27, 2008, 3:09:56 AM8/27/08
to rubyonra...@googlegroups.com

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

Julian Leviston

unread,
Aug 27, 2008, 3:34:33 AM8/27/08
to rubyonra...@googlegroups.com
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/

Fabio Eidi

unread,
Aug 27, 2008, 8:24:33 AM8/27/08
to rubyonra...@googlegroups.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

Reply all
Reply to author
Forward
0 new messages