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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to format an integer such as 1 to a string "001" 2

Status
Not open for further replies.

lydiattc

Programmer
Jun 3, 2003
52
US
Hi,

I need to format a dymanic integer into a string with length of 3. If the integer is 1 then the string is 001, if the integer is 999 then the string is 999. I'm not sure how to accomplish this. Can anyone help?

Thanks a lot!

Lydia
 
MsgBox Right("00" & yourInteger, 3)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks, I think I found a way to solve the problem. It may not be the best way, but it will work.

Thanks.
 
Select Case len(my_string)
Case 1: my_string = "00" & my_string
Case 2: my_string = "0" & my_string
End Select

The early bird gets the worm, but the second mouse gets the cheese.
 
Thanks, 123fakest. This is exactly what I'm doing.
 
So, you prfer 4 lines of code instead of one ?
 
PHV, I really appreciated your help, but I'm a beginner and I'm not sure how your solution will work. When I have time I'll try your solution and I'm sure it's a better solution. Thanks!
 
'you could expand on PHV's suggestion with something like
'i think................

For i = 1 To 99
Wscript.Echo formatZeros("000000", i)
Next

For i = 1 To 9
Wscript.Echo formatZeros("00", i)
Next

Function formatZeros(ByVal strZeros, ByVal yourInteger)
formatZeros = Right(strZeros & yourInteger, Len(strZeros) + 1)
End Function
 
Not the best way, but just an example:

strVar = InputBox("Enter number between 0 & 999","Number","0")

If Len(strVar) <> 3 Then
strVar = "0" & strVar
If Len(strVar) <> 3 Then
strVar = "0" & strVar
End If
End If
wscript.echo strVar
 
or as PHV has already pointed out, you can do all of this with a single line of code provided in his first reply:
Code:
Right("00" & yourInteger, 3)
This will add 2 leading zeroes to yourInteger and then crop the result by taking the 3 right-most figures. Exactly what lydiattc requested

Tony

Spirax-Sarco - steam traps control valves heat exchangers
Sun Villa - Luxury Florida accommodation
 
FesterSXS,
Exactly, but as lydiattc said:
"I'm a beginner and I'm not sure how your solution will work. When I have time I'll try your solution and I'm sure it's a better solution. Thanks!"

I only added my 2 cents worth since it laid it out in a very simple/basic manner.
 
chin up country73, you have introduced the 'beginner' to the Input() function ;-)

carrying on from your idea i would suggest you could have improved on it with

x = "6"
intNumberCharacters = 6
If Len(x) < intNumberCharacters Then
Do While Len(x) <> intNumberCharacters
x = "0" & x
Loop
End If
Msgbox x

At the very least you would have introduced lydiattc to Do Loops with exit conditions.
 
anyway, the only hole i can see is that if yourInteger = ""
in this case all the suggestions presented with return "00"
this is in now way detracting to all excellent suggestions as lydiattc clearly states

'I need to format a dymanic integer'

but seeing as we are talking about vbscript which isnt strongly typed i thought i would mention it....i must be bored
 
mrmovie,
No problems here :)
I'm sure we could throw several different snippets of code together to take care of this, if we really wanted to take the time to do so.
 
mrmovie, what about this ?
MsgBox Right("000" & yourInteger, 3)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Since everyone is adding their two cents, I thought I'd contribute to the knowledge fund.

Code:
Function NumToChar(intValue, intLen)
    NumberToChar = Right(String(intLen, "0") & intValue, intLen)
End Function
Then call this function with something like this...
Code:
MsgBox NumToChar(5, 8)
MsgBox NumToChar(39, 3)
Enjoy!

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
OK how about:

msgbox Right(yourInteger + 1000,3)

Of course it fails for negative numbers, meaning you would need another condition (i.e "if" or "select")

The early bird gets the worm, but the second mouse gets the cheese.
 
Wow, thanks, thanks, thanks! All are great suggestions! Now I feel like I've already been promoted to an expert of formatting an integer! I'm saving this thread for future reference. You guys are great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top