Mar
4
4
Cardinal Endings for Numbers
Filed under: Asides | March 4th, 2006
Released on December 2, 2002
Last Updated: December 2, 2002 9:46 PM
Version: 1
Description
Adds cardinal endings to numbers, like 1st and 2nd, and doesn’t do much else. Has an option or two that might make this useful for you.
Installation/Usage
This adds proper ending for numbers, ie 2nd, 3rd, 8th, with optionally outputting the ending as superscript. Just drop it in your script and have fun. I use it for formatting ages on mullenweg.com but you could really use it for any number. An oldie but a goodie.
Code
nthnum function
<?php
function nthnum ($age,$small=0) { // proper ending for numbers, ie 2nd, 3rd, 8th
$last_char_age = substr("$age", -1);
switch($last_char_age) {
case '1' :
$th = 'st';
break;
case '2' :
$th = 'nd';
break;
case '3' :
$th = 'rd';
break;
default :
$th = 'th';
break;
}
if ($age > 10 && $age < 20) $th = 'th';
if (0 == $small) $niceage = $age.$th;
if (1 == $small) $niceage = $age."<sup>$th</sup>";
return $niceage;
}
?>




