Jump to content

Easy ordinal number suffixes ?


Mahngiel

Recommended Posts

Hey Freaks,

 

Anybody have a shortcut to implement ordinal numbers (1st, 2nd, 3rd) suffixes (sp? suffices??) ?

 

My first thought was to throw a function at it:

<?php
    function suffixizer( $number = 0 ) {
        if( $number == 0 )
            return FALSE;
        elseif( substr($number, -1) == 1 )
            return $suffixed = $number . 'st';
        elseif( substr($number, -1) == 2 )
            return $suffixed = $number . 'nd';
        elseif( substr($number, -1) == 3 )
            return $suffixed = $number . 'rd';
        else
            return $suffixed = $number . 'th';
    }
?>

 

Any thoughts?

 

[edit] forgot about 'th'[/edit]

Link to comment
Share on other sites

  function ordSuffix($n) 
  
  {
      $str = "$n";
      $t = $n > 9 ? substr($str,-2,1) : 0;
      $u = substr($str,-1);
      if ($t==1) 
        return $str . 'th';
      else 
          switch ($u) 
          {
              case 1: return $str . 'st';
              case 2: return $str . 'nd';
              case 3: return $str . 'rd';
              default: return $str . 'th';
          }
  }
  
  echo ordSuffix(1).'<br>';
  echo ordSuffix(11).'<br>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.