Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Adds a suffix to a given number (1st, 2nd, 3rd, ...)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post will appear after it is approved by moderators
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
whoami  
View profile  
 More options Jul 8 2008, 11:29 pm
From: whoami <zeeshan...@gmail.com>
Date: Tue, 8 Jul 2008 20:29:56 -0700 (PDT)
Local: Tues, Jul 8 2008 11:29 pm
Subject: Adds a suffix to a given number (1st, 2nd, 3rd, ...)

// Also, increasing the range above the condition statements
// increases efficiency. That's almost 20% of the numbers
// between 0 and 100 that get to end early.

function number_suffix($number){

    // Validate and translate our input
    if ( is_numeric($number)){

        // Get the last two digits (only once)
        $n = $number % 100;

    }
    else {
        // If the last two characters are numbers
        if ( preg_match( '/[0-9]?[0-9]$/', $number, $matches )){

            // Return the last one or two digits
            $n = array_pop($matches);
        }
        else {

            // Return the string, we can add a suffix to it
            return $number;
        }
    }

    // Skip the switch for as many numbers as possible.
    if ( $n > 3 && $n < 21 )
        return $number . 'th';

    // Determine the suffix for numbers ending in 1, 2 or 3, otherwise
add a 'th'
    switch ( $n % 10 ){
        case '1': return $number . 'st';
        case '2': return $number . 'nd';
        case '3': return $number . 'rd';
        default:  return $number . 'th';
    }

}

Note: All Members are requested to post their views and all about PHP
code and snippets.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google