Adding Suffix (st, nd, rd, th) to Numbers

We are often spoiled by the number of functions that are built into the WL language. This is especially true with it comes to date and time manipulation. I have never worked with an environment that has so many built in functions for dealing with dates. So it came as a bit of a shock to me that there doesn’t appear to be one that adds suffixes to numbers. Just to show off my google skills, these are technically called Ordinal Suffixes.

Well as you probably have guessed, I needed that ability for a project this week. We need to show the day of the month, in “plain English” as part of a paragraph. For Example: “On the 12th day of the month”

A quick google session found the rules for Ordinal Suffixes:

  1. st is used for numbers ending in 1
  2. nd is used for numbers ending in 2
  3. rd is used for numbers ending in 3
  4. EXCEPT (why is there always an EXCEPT??) “teen” numbers, numbers ending in 11, 12, 13 use th
  5. th is used for all other numbers

With those rules in hand, I put together this little function receives an integer as the parameter and returns a string with the number including the suffix.

PROCEDURE NumberSuffix(inNumber is int)

tmpString is string = inNumber
LastDigit is string = Right(tmpString,1)
LastTwoDigits is string = Right(tmpString,2)

IF LastDigit = "1" AND LastTwoDigits <> "11" THEN
    RESULT inNumber + "st"
ELSE IF LastDigit = "2" AND LastTwoDigits <> "12" THEN 
    RESULT inNumber + "nd"
ELSE IF LastDigit = "3" AND LastTwoDigits <> "13" THEN 
    RESULT inNumber + "rd"
ELSE 
    RESULT inNumber + "th" 
END

I turn the number into a string. I then capture both the last digit and the last 2 digits for testing. From there it is just a simple IF THEN statement.

Nothing earth shattering by any means, but maybe it will save someone a few minutes of time.

 

Leave a comment