000 Zero Padding in .vbs

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

e.g
Msgbox ZeroPad("9") will output "009"
Msgbox ZeroPad("11") will output "011"
Msgbox ZeroPad("1000") will output "Error"

Thank you
LVL 1
New_AlexAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Chris BottomleySoftware Quality Lead EngineerCommented:
Try the following:

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

End Function

Open in new window

1
X_layerCommented:
Just one correction:
Function zeropad(str)
    if Len(str) <= 3 then
         zeropad = (Right("000" & str, 3))
    else
         zeropad = "Error"
End Function

Open in new window

0
Chris BottomleySoftware Quality Lead EngineerCommented:
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)

Function ZeroPad(num, charCount)
	ZeroPad = Right(replace(Space(charcount), " ", "0") & num, charCount)
End function

Open in new window

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Chris BottomleySoftware Quality Lead EngineerCommented:
Adding checks for non numeric and more than the stipulated number of characters then ...

Chris
MsgBox ZeroPad("0022", 3)

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

Open in new window

0
Chris BottomleySoftware Quality Lead EngineerCommented:
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")

Function ZeroPad(num)
Const charCount = 3

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

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
New_AlexAuthor Commented:
Thank you sir
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.