100% Based Rating To 5-Star Rating Conversion

Posted at



First for the insults:
A decent part of the b.Sc (or b.A) degraee is devoted to Mathematics,
This is why it is striking me surprised when a fellow dev. asks me how to convert a score from 100 -> 5.


For the two-steps calculation:
1. your_precentage * 5 / 100 or your_precentage * 0.05
for example, if your product scored 78% it is 78 * 0.05 == 3.9
2. we 5 stars bucket (where 1/2 a star is possible too..)
we need to make our data's "roughness", also known as coarseness or granularity (you've lerned it in 5th-grade math)
or more formally: characterized by a high level of detail in a set of data.

each star has either full or half states, so our coarseness or granularity is 1 / 1/2 == 2,
let's get rid of the high-level details by dumping the number over a round function, essentially:
round(your_score * 2) / 2

the whole thing:
round(your_precentage * 0.05 * 2) / 2 == your_5-star-rating_score
or
round(your_precentage * 0.1) / 2 == your_5-star-rating_score

for example: 78% is
round(78 * 0.1) / 2 ===== 4

how round works? it convers values below 1.5 (1.499999, 1.3,..) to 1 and above 1.5 to 2.

for example (#2) 50% is
round(50*0.1)/2 == round(5)/2 == 5/2 == 2.5 in 5-star-rating.


Hope it helps.

Ps.
Shame on you for not knowing this stuff,
you should go back to school (5th grade)