# 000 Zero Padding in .vbs

Hi guys. I am looking for a function that puts 3 zero pads in front of numbers.

e.g

Thank you
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try the following:

Chris
``````Function zeropad(str)

zeropad = (Right("000" & str, 3))

End Function
``````
1
Commented:
Just one correction:
``````Function zeropad(str)
if Len(str) <= 3 then
zeropad = (Right("000" & str, 3))
else
End Function
``````
0
Adding a little flexibility but more work when calling:

The modified function takes two parameters, first the number to prepend, and secondly how many zeroes to be prepennded, (number of characters to be returned in fact).

Chris
``````MsgBox ZeroPad(22, 3)

ZeroPad = Right(replace(Space(charcount), " ", "0") & num, charCount)
End function
``````
0
Adding checks for non numeric and more than the stipulated number of characters then ...

Chris
``````MsgBox ZeroPad("0022", 3)

If isnumeric(num) Then
If Len(num) > charcount Then
Else
ZeroPad = Right(replace(Space(charcount), " ", "0") & num, charCount)
End if
Else
End if
End Function
``````
0
Basically the same but making the three character limit hard so that the call is 'easier' but keeping the simplicity to change the length of the string easily at the code level (within the function)

Chris
``````MsgBox ZeroPad("a2")

Const charCount = 3

If isnumeric(num) Then
If Len(num) > charcount Then
Else
ZeroPad = Right(replace(Space(charcount), " ", "0") & num, charCount)
End if
Else
End If

End Function
``````
0

Experts Exchange Solution brought to you by