Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Adding number suffixes (st, nd, rd, th, etc.)

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Adding number suffixes (st, nd, rd, th, etc.)

Postby graceout » Fri Jan 10, 2003 6:05 am

Is there a cool slick way to determine which suffix goes at the end of a number (1st, 2nd, 3rd, 25th, etc.) besides a dozen if then statements after counting the char length of the number and subtracting all but the last digit to determine the final one, then adding the approriate suffix?

It seems like I should be able to take it's modulus by 10 to determine the last digit. Then (unless the number is between 6 and 20; which all take a 'th'). adding the suffix based on the modulus result.

Any Ideas?

New php-forum User
New php-forum User
Posts: 3
Joined: Wed Oct 30, 2002 9:15 am

Re: Adding number suffixes (st, nd, rd, th, etc.)

Postby WiZARD » Sun Jan 12, 2003 5:56 am

You may download Calendar object from PEAR and see at this
User avatar
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Joan Garnet » Sun Jan 12, 2003 1:47 pm

If you need to determine just up to 12 numbers, then use a simple script:
Code: Select all
switch ($my_number){
     case 1:
         echo $my_number."st";
     case 2:
         echo $my_number."nd";
     case 3:
         echo $my_number."rd";
         echo $my_number."st";

If you need it for larger numbers just tell us.
User avatar
Joan Garnet
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.