Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stars try. Might suck.
<?php
function getStars($number) {
$digits = floor($number);
$remainder = $number - $digits;
// http://php.net/round
$remainder = round($remainder, 0, PHP_ROUND_HALF_UP);
return str_repeat('*', $digits) . (($remainder > 0) ? '½' : '');
}
<?php
/// maybe this is bad.
require 'getstars.php';
for ($i=0; $i<=50;$i++) {
$number = $i / 10;
echo sprintf("%4s", $number) . " => " . getStars($number) . "\n";
}
// 0 =>
// 0.1 =>
// 0.2 =>
// 0.3 =>
// 0.4 =>
// 0.5 => ½
// 0.6 => ½
// 0.7 => ½
// 0.8 => ½
// 0.9 => ½
// 1 => *
// 1.1 => *
// 1.2 => *
// 1.3 => *
// 1.4 => *
// 1.5 => *½
// 1.6 => *½
// 1.7 => *½
// 1.8 => *½
// 1.9 => *½
// 2 => **
// 2.1 => **
// 2.2 => **
// 2.3 => **
// 2.4 => **
// 2.5 => **½
// 2.6 => **½
// 2.7 => **½
// 2.8 => **½
// 2.9 => **½
// 3 => ***
// 3.1 => ***
// 3.2 => ***
// 3.3 => ***
// 3.4 => ***
// 3.5 => ***½
// 3.6 => ***½
// 3.7 => ***½
// 3.8 => ***½
// 3.9 => ***½
// 4 => ****
// 4.1 => ****
// 4.2 => ****
// 4.3 => ****
// 4.4 => ****
// 4.5 => ****½
// 4.6 => ****½
// 4.7 => ****½
// 4.8 => ****½
// 4.9 => ****½
// 5 => *****
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.