n99creations
  View Snippet
Log In!
Not a member? Register
Forgot Password? Resend
ordinalise function
Creator: nathan99
Date Added: May 14, 2007
Rating: 100% (2 of 2 Votes)


Parameters
number:Number - The number that you want to be ordinalised. The function does not directly modify any variables.

Description:
Reads the number parameter and converts it into an ordinalised String (ie: 1st, 2nd, 3rd.. 13th, etc..).

Returns
String - A String value, the ordinalised value of number.

Example
The following demonstrates the usage of the function over the main conditions, displaying every possible result. (Let me know id I missed any exceptions)
Actionscript Code:
function ordinalise(number:Number):String {
  var tmp:String = String(number);
  if (tmp.substr(-2, 2) != "13" && tmp.substr(-2, 2) != "12" && tmp.substr(-2, 2) != "11") {
    if (tmp.substr(-1, 1) == "1") {
      var end:String = "st";
    } else if (tmp.substr(-1, 1) == "2") {
      var end:String = "nd";
    } else if (tmp.substr(-1, 1) == "3") {
      var end:String = "rd";
    }
  }
  if (!end) {
    var end:String = "th";
  }
  return tmp+end;
}
trace(ordinalise(21));// Outputs 21st
trace(ordinalise(102));// Outputs 102nd
trace(ordinalise(33));// Outputs 33rd
trace(ordinalise(13));// Outputs 13th
trace(ordinalise(11));// Outputs 11th
trace(ordinalise(112));// Outputs 112th
trace(ordinalise(1));// Outputs 1st




There are no comments to display.

You must be signed in to post a comment.
Click here to log in.
Click here to register.