Round to the nearest 0.5 | Yang Yang

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 .5 or .0.  Like a lot of languages, PHP will only round to a whole decimal, so you can do .0, .1, .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 .5 or .0, exactly what I'm after.

Explore Other Notes

Newer

It's plant, it's animal, it's bitroph

The idea of an animal that can derive all or some of its energy needs from the sun is one that sci-fi authors have often touched on. Gert has proposed the nomenclature "bitroph" for such a creature, […]
  • 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 […]
  • Murray Adcock.
Journal permalink

Made By Me, But Made Possible By:

CMS:

Build: Gatsby

Deployment: GitHub

Hosting: Netlify

Connect With Me:

Twitter Twitter

Instagram Instragram

500px 500px

GitHub GitHub

Keep Up To Date:

All Posts RSS feed.

Articles RSS feed.

Journal RSS feed.

Notes RSS feed.