dongli2000 2018-06-03 09:45
浏览 61
已采纳

用于转换星级评定值的PHP代码[关闭]

I have given rating values form 1.0 to 6.0. Here 1.0 is the best and 6.0 is the baddest rating. I have to convert the ratings like this. If 1.0 is the given rating value i will have to show 5.0 and IF 6.0 is the given rating value i will have to show 1.0 So if 3.0 is the given rating value i will have to show 2.5 and so on.I am grabbing this 1 to 6 ratings from an external website. Need to convert it to 1 to 5 to my website.

  • 写回答

1条回答 默认 最新

  • dozan0001 2018-06-03 10:31
    关注

    This is more about math than it is about php.

    So let's see, first we need to reverse the values. Because we want change on which end of the rating is worst and best respectively.

    $reverseRating = 7.0 - $inputRating;
    

    This transforms 1.0 into 6.0 and 6.0 into 1.0 etc.

    Now all left to do is scale the values down from 6 stars (input range: 6 - 1 = 5.0) into 5 stars (output range: 5 - 1 = 4.0). For that we will shift everything down to 0, shrink it, then shift it back to 1.

    $outputRating = ($reverseRating - 1.0) / 5.0 * 4.0 + 1.0;
    

    Written as compact one-liner this results in

    $outputRating = (6.0 - $inputRating) * 0.8 + 1.0;
    

    Note

    Your conclusion that the input 3.0 should result in 2.5 is wrong.

    Because the lowest value is 1, not 0, the center value on the input rating scale is not 3, it is 3.5:

    6 5 4 (3.5) 3 2 1

    Same goes for the output center value, it's not 2.5, it is 3:

    1 2 3 4 5

    3.0 on input is actually slightly better than center and will map to 3.4 on output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法