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.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法