Search code examples
rating

Rating calculation


Maybe is a dumb question but I'm figthing with a rating calculation. I've a global rating which is between 0 and 1, the number of ratings, the user rating.

How can I calculate the new rating with :

  • the old global rating
  • the user rating
  • the new and old number of ratings

Any idea ?

Thank you.


Solution

    • Multiply the previous average rating by the previous number of ratings.
    • Add the new rating.
    • Divide by the previous number of ratings plus one to get the new average.

    Careful with your choice of data types or you will lose a little accuracy each time you do this calculation.

    Pseudocode:

    newRating = ((oldRating * previousNumberOfRatings) + userRating) / (previousNumberOfRatings + 1)