Click here to Skip to main content
13,254,754 members (52,586 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 19 Mar 2003

Number Conversion (1 to "1st", etc)

, 19 Mar 2003
Rate this:
Please Sign up or sign in to vote.
An article on Number Conversion from the basic number to the number with a following suffix. (ie, 1 to "1st")


This little function is really simple, but comes in very handy. I have come across this issue many times and usually just dodge it when I can, but finally I had a project that insisted that I format some numbers this way, so alas, here I am.

Using The Code

This is just a basic function call. Send it any number greater than 0 and it will return the number formatted with a "st", "nd", "rd" or "th" after it. Sending it "" will cause an error message to be displayed.

dim iTestNumber 'number to be passed to function
dim sNewNumber  'string returned from function
dim x           'loop incriment

' this function will format a number with it's given suffix. (1st, 2nd, 3rd)<BR><BR>Function BuildNumber(iNum) <BR>    dim sNewNum
    sNewNum = iNum
    if right(iNum, 2) = "11" or right(iNum, 2) = "12" or _
       right(iNum, 2) = "13" then
        sNewNum = sNewNum & "th"
        iNum = right(iNum, 1)

        select case iNum
            case "1"
                sNewNum = sNewNum & "st"
            case "2"
                sNewNum = sNewNum & "nd"
            case "3"
                sNewNum = sNewNum & "rd"
            case ""
                sNewNum = "error"
            case else
                sNewNum = sNewNum & "th"
        end select

    end if
    if sNewNum = "error" then
        response.write "There was an error with the date passed to the "<BR>        response.write "function. The date must be an integer greater "<BR>        response.write "than 0 and not equal to blank. Please use the "<BR>        response.write "back button to fix the problem or contact customer "<BR>        response.write "support. Thank you."
    end if

    buildnumber = sNewNum    
end function

for x = 1 to 100
   iTestNumber = x
   sNewNumber = BuildNumber(iTestNumber)
   response.write("This is the value sent to the function: "<BR>   response.write iTestNumber & "<br>")
   response.write("This is the value returned from the function: "<BR>   response.write "sNewNumber & "<br><HR>")


Points of Interest

Watch out for the little catches such as "11", "12" and "13" all end in "th" rather than "st", "nd" and "rd". I think those are the only exceptions though. It should work for negative numbers, but I'm not sure why you would want to do that. But that's up to you.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Systems Engineer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionAnother way Pin
JBress9-Sep-11 23:47
memberJBress9-Sep-11 23:47 
GeneralError Pin
Eosian1-Aug-05 7:13
memberEosian1-Aug-05 7:13 
GeneralRe: Error Pin
glitch177k1-Aug-05 8:03
memberglitch177k1-Aug-05 8:03 
GeneralRe: Error Pin
Eosian1-Aug-05 8:43
memberEosian1-Aug-05 8:43 
GeneralMore on formatting Pin
allia3-Nov-03 10:54
memberallia3-Nov-03 10:54 
GeneralRe: More on formatting Pin
glitch177k3-Nov-03 11:11
memberglitch177k3-Nov-03 11:11 
GeneralThanks! Pin
allia3-Nov-03 11:14
memberallia3-Nov-03 11:14 
General11, 12 and 13 Pin
Simon Armstrong21-Mar-03 0:53
sussSimon Armstrong21-Mar-03 0:53 
GeneralRe: 11, 12 and 13 Pin
glitch177k21-Mar-03 5:05
memberglitch177k21-Mar-03 5:05 
GeneralRe: 11, 12 and 13 Pin
Ramona25-Mar-03 8:26
memberRamona25-Mar-03 8:26 
GeneralRe: 11, 12 and 13 Pin
glitch177k25-Mar-03 9:07
memberglitch177k25-Mar-03 9:07 
GeneralRe: 11, 12 and 13 Pin
Ramona25-Mar-03 9:47
memberRamona25-Mar-03 9:47 
GeneralRe: 11, 12 and 13 Pin
Ramona25-Mar-03 10:02
memberRamona25-Mar-03 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 20 Mar 2003
Article Copyright 2003 by glitch177k
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid