我有一行代码,从本质上计算转换率。然而,如果这是一个新的运动,转换率将显示为南%,而不是0.00% (我有2的精度)。
我试着添加
rescue 0.00 然而,最终还是没有成功。下面是我的代码片段:
number_to_percentage((campaign.favorites.where(:owner_id => current_user.followers.map(&:follower_id)).count.to_f / campaign.favorites.where(favorited: true).count.to_f * 100), precision: 2) rescue 0.00对如何做到这一点有什么线索吗?
发布于 2015-03-23 19:57:35
fav = campaign.favorites.where(favorited: true)
val = fav.present? ? campaign.favorites.where(:owner_id => current_user.followers.map(&:follower_id)).count.to_f / fav.count.to_f) : 0.0
number_to_percentage(val)https://stackoverflow.com/questions/29219088
复制相似问题