Division to percentage in Ruby on Rails

蓝咒 提交于 2020-01-03 08:38:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!