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

Add leading Zeros

Status
Not open for further replies.

jadec

MIS
Jan 22, 2004
87
US
Hi,
Sorry for asking again. I have a number, I want to add leading zeros to make it as a 6 length long char field. Other then using do loop, is there any function that I can use?

Thanks
 
Assuming you know the numbers will be less than 1 million (e.g. >=999999...which is max 6 digit number) then the following should work (note the replace command):

Code:
dim x, strX
x= 49999
strX = replace(space(6-len(CStr(x))), " ", "0") & x
msgbox strX

TR
 
I was late I see on a refresh

Code:
txtJDEnum = string(6 - len(JDEnum) , "0") & cstr(JDEnum)
Response.Write(txtJDEnum)
Response.Write(cLng(txtJDEnum))


Reasoning I'm posting anyhow and showing you this one is the cLng() hit on the value again. You're going to need to do some sort of conversion to numeric to utilize the value as a numeric value again, so be careful of that. It will take surpress the zeros once again.

TJRTech -- never thought of a replace. nice!


___________________________________________________________________

The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
 
O' forgot the link to the reference on that method
Add leading zeros discussion

show credit where due [smile]

___________________________________________________________________

The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
 
Code:
Response.Write(StandardNum(445, 7))

Function StandardNum(iNum, iLength)

	'Throw error
	If Not IsNumeric(iLength) Then
		StandardNum = -1
	End If

	
	'Throw error
	If Not IsNumeric(iNum) Then
		StandardNum = -1
	End If
	
	While(Len(iNum) <= (iLength - 1))
		iNum = (0 & iNum)
	Wend
	
	StandardNum = iNum
	
	
End Function
- J
 
almost 4got.

first param is the actualy number, second param is the total length you desire.

so if the total length should be 7 (ie, 0004567) you would call StandardNum(4567, 7).

 
Thanks everyone! they are all very helpful.

onpnt, it works. And does cLng works same as CDbl ?

Thanks
 
any data type conversion function that you use from the string to the numberic will do the same. It's actually a very common way of surpressing leading zeros if you ever need that usage.

Surprisingly after throwing those methods together the String() method was the quickest. And even more surprising was the predefined function with the loop in there and the Replace() method were about equal.

I'm off to see why that is and find out if my time tests were possibly off.

___________________________________________________________________

The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
 
well, just because I must be a geek I'll post this. There is no way either use of any of the three methods will in any way slow down execusion unless you use these on SQL query returns of mass data

Anyhow here's what I get out of testing the three.
(in milliseconds on 10000 iterations)
elapsed on string() usage= 3.125
elapsed on replace usage()= 4.6875
elapsed Function call -- conditioning (nested loop)= 9.375

Now I'm going to rethink my status of trying out for revenge of the nerds. [lol]

___________________________________________________________________

The answer to your ??'s may be closer then you think. faq333-3811
Join the Northern Illinois/Southern Wisconsin members in Forum1064
 
Hi onpnt, I like to use string(). because the syntax is simple. I'm glad it is the quickest one. thanks!
 
Yeah, string would be the way to go...should be quicker than doing "space" wwithin a "replace". Frankly, I am surprised the space and replace solution faired that well in speed when compared to string().

I totally forgot about string.

TR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top