Kingy Posted January 23, 2008 Share Posted January 23, 2008 With my rank i am outputting something like You are ranked XXst/nd/rd/th eg: 1st, 2nd, 3rd, 4th if(($rank == 1) || ($rank == 21) || ($rank == 31)) { $suffix = "st"; } elseif (($rank == 2) || ($rank == 22) || ($rank == 32)) { $suffix = "nd"; } elseif (($rank == 3) || ($rank == 23) || ($rank == 33)) { $suffix = "rd"; } else { $suffix = "th"; } i have that and it works, but is there simpler way of doing it so as my numbers grow i don't have to keep adding if $rank == 41,51,61 etc etc Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/ Share on other sites More sharing options...
tippy_102 Posted January 23, 2008 Share Posted January 23, 2008 You have to test only the last digit of each number, anything else (and any of the "teens") will end in "th". Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446639 Share on other sites More sharing options...
Kingy Posted January 23, 2008 Author Share Posted January 23, 2008 yeah i understand that, so what sort of code would i use to do that Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446642 Share on other sites More sharing options...
Ken2k7 Posted January 23, 2008 Share Posted January 23, 2008 function last_digit(num){ return (num > 10)? last_digit(num%10) : num; } I think that would work Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446650 Share on other sites More sharing options...
sasa Posted January 23, 2008 Share Posted January 23, 2008 try <?php function my_suffix($b){ if ($a<0) $a = -$b; else $a =$b; $s = array('th', 'st', 'nd'); $su = $a % 10; if (((int)($a / 10)) % 10 == 1) $su = 0; if ($su > 2) $su = 0; return $s[$su]; } echo my_suffix(191); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446651 Share on other sites More sharing options...
Ken2k7 Posted January 23, 2008 Share Posted January 23, 2008 Ugh, I forgot the $ in variables. :S Well you get the idea. @sasa, what's $a? Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446654 Share on other sites More sharing options...
Kingy Posted January 23, 2008 Author Share Posted January 23, 2008 woah that looks interesting, and works too, but doesn't contain 'rd' for 3rd how do u fit rd in there :s Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446660 Share on other sites More sharing options...
kenrbnsn Posted January 23, 2008 Share Posted January 23, 2008 I did a search in Google and came up with this Ordinal number printer function <?php function ordinal($number) { // when fed a number, adds the English ordinal suffix. Works for any // number, even negatives if ($number % 100 > 10 && $number %100 < 14) $suffix = "th"; else { switch($number % 10) { case 0: $suffix = "th"; break; case 1: $suffix = "st"; break; case 2: $suffix = "nd"; break; case 3: $suffix = "rd"; break; default: $suffix = "th"; break; } } return "${number}<SUP>$suffix</SUP>"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446662 Share on other sites More sharing options...
sasa Posted January 23, 2008 Share Posted January 23, 2008 ups try <?php function my_suffix($b){ if ($a<0) $a = -$b; else $a =$b; $s = array('th', 'st', 'nd', 'rd'); $su = $a % 10; if (((int)($a / 10)) % 10 == 1) $su = 0; if ($su > 3) $su = 0; return $s[$su]; } echo my_suffix(191); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87325-solved-numbering/#findComment-446663 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.