Jump to content

Numeric Conversion?


deth4uall

Recommended Posts

I don't think there's a native function for this, AFAIK.

 

function numSuffic($num){
    switch(substr($num,-1) ){
        case 1 : return $num.'st';
        case 2 : return $num.'nd';
        case 3 : return $num.'rd';
        default : return $num.'th';
    }
}

echo "Congratulations you finished in " . numSuffic(102) . " place.";
?> 

Link to comment
https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842744
Share on other sites

What about 11 - 13?

 

Right, sorry:

 

function numSuffic($num){
    switch(substr($num,-1) ){
        case 1 : return substr($num, -2, 1)==1 ? $num . 'th' : $num.'st';
        case 2 : return substr($num, -2, 1)==1 ? $num . 'th' : $num.'nd';
        case 3 : return substr($num, -2, 1)==1 ? $num . 'th' : $num.'rd';
        default : return $num.'th';
    }
}
for($x=1; $x{
   echo "Congratulations you finished in " . numSuffic($x) . " place.
";
}
?> 

 

Link to comment
https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842757
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.