Skip to content

Instantly share code, notes, and snippets.

@grough
Created October 6, 2011 18:57
Show Gist options
  • Save grough/1268290 to your computer and use it in GitHub Desktop.
Save grough/1268290 to your computer and use it in GitHub Desktop.
Convert a percent rating to a five star grade
0 .....
1 ..... 21 X.... 41 XX... 61 XXX.. 81 XXXX.
2 ..... 22 X.... 42 XX... 62 XXX.. 82 XXXX.
3 ..... 23 X.... 43 XX... 63 XXX.. 83 XXXX.
4 ..... 24 X.... 44 XX... 64 XXX.. 84 XXXX.
5 /.... 25 X/... 45 XX/.. 65 XXX/. 85 XXXX/
6 /.... 26 X/... 46 XX/.. 66 XXX/. 86 XXXX/
7 /.... 27 X/... 47 XX/.. 67 XXX/. 87 XXXX/
8 /.... 28 X/... 48 XX/.. 68 XXX/. 88 XXXX/
9 /.... 29 X/... 49 XX/.. 69 XXX/. 89 XXXX/
10 /.... 30 X/... 50 XX/.. 70 XXX/. 90 XXXX/
11 /.... 31 X/... 51 XX/.. 71 XXX/. 91 XXXX/
12 /.... 32 X/... 52 XX/.. 72 XXX/. 92 XXXX/
13 /.... 33 X/... 53 XX/.. 73 XXX/. 93 XXXX/
14 /.... 34 X/... 54 XX/.. 74 XXX/. 94 XXXX/
15 X.... 35 XX... 55 XXX.. 75 XXXX. 95 XXXXX
16 X.... 36 XX... 56 XXX.. 76 XXXX. 96 XXXXX
17 X.... 37 XX... 57 XXX.. 77 XXXX. 97 XXXXX
18 X.... 38 XX... 58 XXX.. 78 XXXX. 98 XXXXX
19 X.... 39 XX... 59 XXX.. 79 XXXX. 99 XXXXX
20 X.... 40 XX... 60 XXX.. 80 XXXX. 100 XXXXX
<?php
function percent_to_stars($p, $star, $half, $empty)
{
$stars = floor((round($p / 10) / 2));
$halves = round($p / 10) % 2;
$empties = 5 - $stars - $halves;
return str_repeat($star, $stars)
. str_repeat($half, $halves)
. str_repeat($empty, $empties);
}
def percent_to_stars(p, star="X", half="/", empty=".")
stars = ((p / 10.0).round / 2).floor
halves = (p / 10.0) % 2
return star * stars
+ half * halves
+ empty * (5 - stars - halves)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment