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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

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?

Thanks

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

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
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

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";
         break;
     case 2:
         echo $my_number."nd";
         break;
     case 3:
         echo $my_number."rd";
         break;
     default:
         echo $my_number."st";
}

If you need it for larger numbers just tell us.
bye!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest