I'm still chipping away at getting a review section working. Part of that has been finding a way to automate the calculation of series ratings from individual ratings. Not the hardest equation in the world: take the sum of the ratings and divide by the total number of reviews. However, I want to display this using a star (⭐) system, which means I can't have any decimal other than
.0. Like a lot of languages, PHP will only round to a whole decimal, so you can do
.01 etc. using
round(), but nothing in between. Luckily, Yang has a solution:
$rating = round((sum rating / ratings count) * 2) / 2;
It's a bit fiddly, but it means that every rating it outputs is either
.0, exactly what I'm after.