Outputing Ordinal Numbers in ASP
ASP Best Practices
Converting regular numbers to Ordinal numbers, as in 1st, 2nd, 3rd and so on.
Date : 2006-04-04
Every language has ordinal and cardinal numbers. Cardinal numbers are the ones we do math with. Ordinal numbers answer the question "Which One?". For instance "Which beer was your favorite?", "The 12th one.". Twelfth, or 12th is an ordinal number. It is much more "real world" than computers usually get. Converting numbers to ordinal for the purpose of displaying, especially in the case of dates, can go a long way to make a site look professional. We always say it's the "little things" that make greatness. Well, this is one of the little things. It's not difficult to do, it just takes a minute but it can make a big difference. So here is how to convert am english number into ordinal.
sNum = cStr(i)
iNum = i mod 10
select case iNum
sExt = "st"
sExt = "nd"
sExt = "rd"
case else '4,5,6,7,8,9,0
sExt = "th"
' Take care of those crazy teens
if i > 10 and i <20 then sExt = "th"
sNum = sNum & sExt
intToOrdinal = sNum
As a helpful user pointed out I had failed to take into account the teens so that has been updated in the above function.
It all comes down to what the last number in the value is. So we test for what the last number is in the string of numbers and add the correct suffix for that number. Simple enough. Like was mentioned, the most useful purpose for this is for date formatting so I updated the ASP Extended Date Formatting function to take advantage of ordinal numbering for trully professional looking date output. Take a look: ASP Extended Date Formatting
Feel free to use this function, just be sure to let us know if you improve on it.
You've got a flaw here:
Which is ironic since you even used "12th" in your writeup above, but your code doesn't handle that.
Also, modulus arthimetic would save the conversion from a number to string back to a number to get the value of the last digit. Modulo 10 of any number will yield the last digit of that number.
What, it's not supposed to be Elevenst? Thanks for pointing that out Steve, I fixed the function where it handles the teens correctly.