Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Leading Zeros with FormatNumber 2

Status
Not open for further replies.

uncgis

MIS
Apr 9, 2004
58
0
0
US
I am accessing ID numbers that have leading zeros (ex. 00500. Whenever I pull the data it prints 500 and not the leading zeros. I have tried using the code...

<%= FormatNumber(rsAddComments("ClassID"),0,1)%>

But it give the error "Invalid procedure call or argument: 'FormatNumber'"

Does anyone know how to fix this....

Thanks...
 
Well, two things here... to set the leading zero to true, it's -1, not 1. Secondly, leading zero refers to decimal values, not integers with additional 0's in front. A possible solution may be to convert it to a string value, or if the field length is always the same, you could add 0's to the front till it's the correct length... or yet another way is to check the length of it in the db and then make sure it's still that same length after you get it out.
 
Though I've written several little zero-padding functions over the years, this one just occurred to me, and is the simplest I can think of:
Code:
'intValue is the number you want to pad
'Assumes a desired length of 5
Function ZeroPadLeft(intValue)
    ZeroPadLeft = Right("00000" & intValue, 5)
End Function
You'd put the function in ASP tags somewhere on your page (I prefer the bottom, but anywhere is fine). Then in your code you'd use it like this (using your example code):
Code:
<%=ZeroPadLeft(rsAddComments("ClassID"))%>
 
An alternate version where you can pass the desired length would look like this:
Code:
'intValue is the number you want to pad
'intLength is the length you want to pad it out to
'Note that if the length of intValue is greater than intLength
'then intValue will be shortened to the rightmost intLength characters
Function ZeroPadLeft(intValue, intLength)
    ZeroPadLeft = Right(Replace(Space(intLength), " ", "0") & intValue, intLength)
End Function
or a version that won't chop your value, returning the whole thing if it's already long enough
Code:
'intValue is the number you want to pad
'intLength is the length you want to pad it out to
'Returned as a string either way
Function ZeroPadLeft(intValue, intLength)
    If Len(intValue) < intLength Then
       ZeroPadLeft = Right(Replace(Space(intLength), " ", "0") & intValue, intLength)
    Else
        ZeroPadLeft = CStr(intValue)
    End If
End Function
 
Thanks for everyone that replied....I used Genimuse's code and it is working perfect...Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top