deth4uall Posted May 26, 2009 Share Posted May 26, 2009 I am looking for a function that allows me to convert 1, 2, 3, 4, 5 from int to str and look like 1st, 2nd, 3rd, 4th, 5th etc... Any ideas? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/159783-numeric-conversion/ Share on other sites More sharing options...
Maq Posted May 26, 2009 Share Posted May 26, 2009 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."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842744 Share on other sites More sharing options...
deth4uall Posted May 26, 2009 Author Share Posted May 26, 2009 What about 11 - 13? Quote Link to comment https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842748 Share on other sites More sharing options...
lonewolf217 Posted May 26, 2009 Share Posted May 26, 2009 11th, 12th, 13th, etc are all taken care of by the default case Quote Link to comment https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842750 Share on other sites More sharing options...
Maq Posted May 26, 2009 Share Posted May 26, 2009 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. "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159783-numeric-conversion/#findComment-842757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.