Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division to percentage in Ruby on Rails

If

@prescribed_wod_count = @user.workouts.rx_workouts.count returns 4

and

@user_workout_count = @user.workouts.count returns 26

how come

<%= number_to_percentage(@prescribed_wod_count / @user_workout_count) %> returns 0.000% and not 15% ?

like image 892
bgadoci Avatar asked Nov 30 '10 06:11

bgadoci


1 Answers

It does integer division, before you call number_to_percentage.

You want

<%= number_to_percentage(@prescribed_wod_count.to_f / @user_workout_count) %>

to force it to do floating-point

like image 152
Andrew McGuinness Avatar answered Sep 21 '22 17:09

Andrew McGuinness