views:

2818

answers:

6

I need to have a string, based on an integer, which should always have 5 digits.

Example:

myInteger = 999
formatedInteger = "00999"

What is the best way of doing this in classic ASP?

+1  A: 

Something like this is what I've seen most of the time:

function PadNumber(number, width)
   dim padded : padded = cStr(number)

   while (len(padded) < width)
       padded = "0" & padded
   wend

   PadNumber = padded
end function

PadNumber(999, 5) '00999
Jonathan Lonowski
I like how this inherently handles the case where number has more digits than the desired width.
Mike Henry
+2  A: 

You can use string manipulation functions for this.

This assumes classic ASP with VBScript (original version of the answer).

Const NUMBER_DIGITS = 5

Dim myInteger
Dim formatedInteger

myInteger = 999
formatedInteger = Right(String(NUMBER_DIGITS, "0") & myInteger, NUMBER_DIGITS)

Here an optimized version, wrapped in a function, offering variable width padding:

Const NUMBER_PADDING = "000000000000" ' a few zeroes more just to make sure '

Function ZeroPadInteger(i, numberOfDigits)
  ZeroPadInteger = Right(NUMBER_PADDING & i, numberOfDigits)
End Function

' Call in code: '

strNumber = ZeroPadInteger(myInteger, 5)
Tomalak
Stefan
Yes, I'll include that and wrap it in a function.
Tomalak
I use a similar function but add a check for the case where the number has more digits than desired. I do like how Jonathan Lonowski's function handles this inherently.
Mike Henry
A: 

Really, you should ask yourself why you might want this.

If this is for display purposes then it's probably best to apply a string formatting function (there will be one) to your integer, at the point of display.

On the other hand, if you need it for internal processing, i.e. you're always expecting five digits in a loop or whatever, but you're not expecting to do arithmetic on the value, then convert the integer to a string first and then do any processing.

In short, convert the integer variable to a string and store in a new variable, then use that.

Jonathan Swift
A: 

Stefan, that just gave me an error: incorrect type

I've done this small function

Function formatLeadingZeros(num, charSize)

num=cstr(num)
lenNum = len(num)

if charSize > lenNum then
    zerosToAdd = charSize - lenNum

    for i=1 to zerosToAdd
     tempStr = tempStr & "0"
    next
    tempStr = tempStr & num

end if

formatLeadingZeros = tempStr 

End function

Can I optimize it?

Armadillo
I have deleted my answer, I remembered wrong and my solution only worked in VB6, not Classic Asp.
Stefan
A: 

Ok, now I have 3 options: use my function, Jonathan Lonowski suggestion or use Tomalak suggestion.

Which one is more optimized??

Thanks

Armadillo
It depends on what you want to do. I guess with a function wrapped around my code my solution would be the fastest, because it does not do character-by-character processing in a loop.
Tomalak
Stefan
A: 

Try this for a one-liner (well, two with error prevention):

function padZeroDigits(sVariable, iLength)
    if (iLength <= len(sVariable)) then padZeroDigits = sVariable : exit function : end if
    padZeroDigits = string(iLength - len(sVariable),"0") & sVariable
end function
Cirieno