12,510,836 members (57,670 online)
alternative version

67.1K views
22 bookmarked
Posted

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

, 19 Mar 2003
 Rate this:
An article on Number Conversion from the basic number to the number with a following suffix. (ie, 1 to &quot;1st&quot;)

Introduction

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)

Function BuildNumber(iNum)
dim sNewNum
sNewNum = iNum

if right(iNum, 2) = "11" or right(iNum, 2) = "12" or _
right(iNum, 2) = "13" then
sNewNum = sNewNum & "th"
else
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 "
response.write "function. The date must be an integer greater "
response.write "than 0 and not equal to blank. Please use the "
response.write "back button to fix the problem or contact customer "
response.write "support. Thank you."
response.End
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: "
response.write iTestNumber & "<br>")
response.write("This is the value returned from the function: "
response.write "sNewNumber & "<br><HR>")
next

response.end

```

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.

A list of licenses authors might use can be found here

Share

 Systems Engineer United States
No Biography provided

You may also be interested in...

 Pro Pro

 First Prev Next
 Another way JBress9-Sep-11 22:47 JBress 9-Sep-11 22:47
 Error Eosian1-Aug-05 6:13 Eosian 1-Aug-05 6:13
 Re: Error glitch177k1-Aug-05 7:03 glitch177k 1-Aug-05 7:03
 Re: Error Eosian1-Aug-05 7:43 Eosian 1-Aug-05 7:43
 More on formatting allia3-Nov-03 9:54 allia 3-Nov-03 9:54
 Re: More on formatting glitch177k3-Nov-03 10:11 glitch177k 3-Nov-03 10:11
 Thanks! allia3-Nov-03 10:14 allia 3-Nov-03 10:14
 11, 12 and 13 Simon Armstrong20-Mar-03 23:53 Simon Armstrong 20-Mar-03 23:53
 Re: 11, 12 and 13 glitch177k21-Mar-03 4:05 glitch177k 21-Mar-03 4:05
 Re: 11, 12 and 13 Ramona25-Mar-03 7:26 Ramona 25-Mar-03 7:26
 Re: 11, 12 and 13 glitch177k25-Mar-03 8:07 glitch177k 25-Mar-03 8:07
 Re: 11, 12 and 13 Ramona25-Mar-03 8:47 Ramona 25-Mar-03 8:47
 Re: 11, 12 and 13 Ramona25-Mar-03 9:02 Ramona 25-Mar-03 9:02
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Sep-16 23:17 Refresh 1

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

Web02 | 2.8.160929.1 | Last Updated 20 Mar 2003