Ruby on Rails中的百分比

时间:2010-11-30 06:42:44

标签: ruby-on-rails ruby math division

如果

@prescribed_wod_count = @user.workouts.rx_workouts.count返回4

@user_workout_count = @user.workouts.count返回26

怎么来

<%= number_to_percentage(@prescribed_wod_count / @user_workout_count) %>返回0.000%而不是15%?

1 个答案:

答案 0 :(得分:18)

在调用number_to_percentage之前进行整数除法。

你想要

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

强制它做浮点