问题
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% ?
回答1:
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
来源:https://stackoverflow.com/questions/4311190/division-to-percentage-in-ruby-on-rails