Convert rating to percentage in 5 star rating system

16.1k views Asked by At

I'm having a 5 star rating system in one of my projects. And i would like to convert each star in to percentage value.

This is my database

star1

star2

star3

star4

star5

tot_stars

average

calculation i use to get rating

$cal = ($star1*1 + $star2*2 + $star3*3 + $star4*4 + $star5*5)/$total_stars;

$avg = number_format($cal, 2);

What i want to know is what is the math to get each star as a percentage

Example: Yelp.com do this in their website

3

There are 3 answers

0
Mark Reed On BEST ANSWER

If your goal is to get the percentage of ratings that are (for example) 1-star, then just divide star1 by the number of ratings (which I guess is tot_stars, although the name seems to imply otherwise). That gets you a proportion; multiply by 100 to get a percentage.

So, for example, letting the database do the math:

SELECT star1, star1 * 100 / tot_stars AS percent1, 
       star2, star2 * 100 / tot_stars AS percent2, 
       star3, star3 * 100 / tot_stars AS percent3, 
       star4, star4 * 100 / tot_stars AS percent4, 
       star5, star5 * 100 / tot_stars AS percent5
  FROM starsTable

or doing it in PHP:

<?php     
// Example data
$star1 = 1;
$star2 = 5;
$star3 = 7;
$star4 = 10;
$star5 = 8;

$tot_stars = $star1 + $star2 + $star3 + $star4 + $star5;

for ($i=1;$i<=5;++$i) {
  $var = "star$i";
  $count = $$var;
  $percent = $count * 100 / $tot_stars;
  for ($j=1;$j<=5;++$j) {
    echo $j <= $i ? "☆ " : "  ";
  }
  printf("\t%2d (%5.2f%%)\n", $count, $percent,2);
}
?>

Which outputs this:

☆            1 ( 3.23%)
☆ ☆          5 (16.13%)
☆ ☆ ☆        7 (22.58%)
☆ ☆ ☆ ☆     10 (32.26%)
☆ ☆ ☆ ☆ ☆    8 (25.81%)
0
Lajos Arpad On

I propose a solution, which is a function, which gets an array, which has the stars as values. We calculate to totalvalues and then calculate the percentage one-by-one. We return the percentages. Each percentage will correspond to the star in the input having the very same index.

function getPercentages($inputValues) {
    $totalValues = 0;
    foreach ($inputValues as $inputValue) {
        $totalValues += $inputValue;
    }
    $outputValues = array();
    foreach ($inputValues as $key => $inputValue) {
        $outputValues[$key] = 100 * $inputValue / $totalValues;
    }
    return $outputValues;
}
0
Ionic On

Well I would suggest to do the math in your database and let it return you a float value. This way you can easily display the stars in those "percentage" style.

Example:

SELECT (CAST(
          (star1*1) + (star2*2) + (star3)*3 + (star4*4) + (star5*5) 
        as float)/15) as percentageStars

Just as an example for your display:

  • 5 Stars: 80-100
  • 4 Stars: 60-80
  • 3 Stars: 40-60
  • 2 Stars: 20-40
  • 1 Star: 0-20

Due to the fact that you know that 1 star spans 20 Points, you can easily take 1 point as 5% for a fillfactor for the answered star. I think you will try to transform it afterwards to an css property to show the star colored, right?

Example: You get back a rating of... well lats say 4.8. This means you could just use the full value as total stars, which means 4. Afterwars you can use modulo (%1) to get the rest value. This rest value can be multiplied by 100 to get a percentage fill factor for your css for example.

Formula: 4.8%1 = 0.8 * 100 = 80

Hopefully this will help you?