Getting The Ordinal Suffix Of A Number Using ColdFusion

Published: {ts '2012-04-24 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/getting-the-ordinal-suffix-of-a-number-using-coldfusion/

I would consider this a very simple problem to solve. However after recently coming across a solution for this in the wild that was shall we say less than optimal, I decided that maybe it was worth sharing my solution for any new developers.

The Complete Function

First here is my solution to the problem. See below for a break down.

Breaking It Down

So what is it doing here. First we check if the number ends with two digits representing values between "10" and "19" by using the string functions mid() and len() to get the second last digit of the number. If true then the suffix is always "th".

For every other number greater than zero we get the last digit of the number using the right() string function.

Based on the value of that last digit we set the suffix accordingly.

Alternative Solution

Here is an alternative solution as suggestion by Matt Gutting. I tested it against my solution and it does give a small performance increase when you get up to several thousand calls. On a loop of 10,000 I got a difference of 300ms (mine) vs 180ms (matts).