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
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 istot_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:
or doing it in PHP:
Which outputs this: