我建立5星评级系统。问题来自用户对照片评分并最终 br 将返回超过100%,我怎样才能确保它以百分比回报并且不超过100%
br = ( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes)
答案 0 :(得分:1)
假设avg_rating
和this_rating
都不超过100%,并且所有值都是非负数,那么您的公式永远不会超过100%。
br = ( (avg_n * avg_r) + (this_n * this_r) ) / (avg_n + this_n) <=
<= ( (avg_n * 100%) + (this_n * 100%) ) / (avg_n + this_n) =
= 100% * (avg_n + this_n) / (avg_n + this_n) =
= 100%
如果avg_rating
和this_rating
未按百分比衡量(例如,它们是星数0-5),则应首先将它们转换为百分比。