Random Code
Posted by tim in I am BORED! on December 24, 2006

A while back there was a contest on Preempted to see if anyone could figure out how to determine the proper suffix to a number (1st, 2nd, 3rd, 4th, etc.) with their own code. I don't remember what I submitted for the challenge, but I do remember it was bloated and really ugly.

I just decided to try it again, and I came up with the following:

function MakeSuffix($num) { $lastDigit = substr($num, -1, 1); if ($lastDigit == 1 && substr($num, -2, 2) != 11) $out = 'st'; elseif($lastDigit == 2 && substr($num, -2, 2) != 12) $out = 'nd'; elseif($lastDigit == 3 && substr($num, -2, 2) != 13) $out = 'rd'; else $out = 'th';

return $out;

}

 
Comments
There aren't any comments here yet. Maybe you should add one!
Add a comment