I found myself in need of a function to get the ordinal suffix for any number. Naturally, I googled and found some examples out there but they all seemed to have many lines of code and I felt an uncontrollable urge to see if I could condense it down to one line. I jumped on over to trycf.com and fiddled around for a bit and I came up with this:

<cfscript>

function OrdinalSuffix(n) {

return arrayfind([1,2,3],n%10)&&!arrayfind([11,12,13],n%100)?listgetat('st,nd,rd',n%10):'th';

}

x = 0;

while (x LT 1001) {

x++;

WriteOutput(numberformat(x)&OrdinalSuffix(x)&'<br>');

}

</cfscript>

function OrdinalSuffix(n) {

return arrayfind([1,2,3],n%10)&&!arrayfind([11,12,13],n%100)?listgetat('st,nd,rd',n%10):'th';

}

x = 0;

while (x LT 1001) {

x++;

WriteOutput(numberformat(x)&OrdinalSuffix(x)&'<br>');

}

</cfscript>

As a side note: While I was fiddling around with this completely unnecessary effort (that was likely fueled by my self diagnosed OCD), I did notice an interesting difference between Adobe CF and Lucee that I had not noticed before. It appears that in Lucee you can immediately use a member function when an array is written using implicit notation, but in Adobe CF you have to define the variable separately before you can use the member functions. For example in Lucee I was able to do this (which threw an error in Adobe CF):

<cfscript>

function OrdinalSuffix(n) {

return [1,2,3].find(n%10)&&![11,12,13].find(n%100)?['st','nd','rd'][n%10]:'th';

}

x = 0;

while (x LT 1001) {

x++;

WriteOutput(numberformat(x)&OrdinalSuffix(x)&'<br>');

}

</cfscript>

function OrdinalSuffix(n) {

return [1,2,3].find(n%10)&&![11,12,13].find(n%100)?['st','nd','rd'][n%10]:'th';

}

x = 0;

while (x LT 1001) {

x++;

WriteOutput(numberformat(x)&OrdinalSuffix(x)&'<br>');

}

</cfscript>

That code gave me the warm fuzzies, because it was 14 characters shorter than what I ended up needing to get it to work on one line with Adobe CF. I also just really like having the option to avoid creating variables I don't need. Here is a simplified example of what I am talking about:

<cfscript>

*
//In Adobe CF I need to do this to use a member function*

a=[1,2,3];

WriteOutput(a.find(2));

*
//This works great in Lucee, but errors in Adobe CF*

WriteOutput([1,2,3].find(2));

</cfscript>

a=[1,2,3];

WriteOutput(a.find(2));

WriteOutput([1,2,3].find(2));

</cfscript>

Strangely though in ACF you CAN immediately use the member function on a string:

<cfscript>

WriteOutput("123".find(2));

</cfscript>

WriteOutput("123".find(2));

</cfscript>

It turns out that there is a bug tracker for this issue in Adobe CF so please head on over and vote for them to fix it:

There are no comments for this entry.

[Add Comment]