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

remove leading Zero's

Status
Not open for further replies.

shiggyshag

Programmer
Dec 14, 2001
227
GB
Hi

If I have a number say 0365 i wnat to just display 365
or if i have say 02345 to just display 2345 or if i have 0001 to just display 1

Any help would be great

Cheers
 
function removeZero(intNum)
newNum = Replace(intNum,"0","")
removeZero = newNum
End function


dim newNum, intNum1, intNum2, intNum3
intNum = "0365"
intNum2 = "02345"
intNum3 = "0001"
response.write removeZero(intNum) & " " & removeZero(intNum2) & " " & removeZero(intNum3)
---------------------------------------
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
In order for it to be 0-filled it has to be a string (numbers don't store starting 0's). So if you use cInt then it will convert it to an integer instead of a 0-filled numeric string. Then when you print it you wouldn't have the 0-fill.
<%
Dim myNum
'0 filled string
myNum = &quot;0339&quot;
Response.Write myNum & &quot; becomes &quot; & cInt(myNum) & &quot;<br>&quot;

'0 filled number
myNum = 0339
Response.Write myNum & &quot; becomes &quot; & cInt(myNum) & &quot;<br>&quot;
%>
Notice in the second case that the number isn't zero-filled even before you cInt it.

-Tarwn
-tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
you know what that's called?
onpnt trying to find a way for a snipp of code do what a little built in function already does

[lol] ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
The function to remove zero's above works great but it takes out all of the zero's not just the leading ones. For instance 0045067n becomes 4567n. Is there anyway to just get rid of the leading zeros but leave any zero occuring later on in the string intact. This really has me stumped, can anyone help
cheers
alex
 
Couple ways you could do it. You could loop through one character at a time throwing away zeros until yo hit a non-zero or you could use a regular expression to get the longest string that doesn't start with a zero.
Code:
Function RemovePadding(aStr)
   Dim i
   For i = 1 to Len(aStr)
      If left(aStr,1) = &quot;0&quot;
         aStr = Right(aStr,Len(aStr)-1)
      Else
         Exit For
      End If
   Next
   RemovePadding = aStr
End Function

Function RemovePadding2(aStr)
   Dim regex
   Set regex = New RegExp
   regex.pattern = &quot;(0*)([^0].*)$&quot;

   aStr = regex.Replace(aStr,&quot;$2&quot;)
   Set regex = Nothing
   RemovePadding2 = aStr
End Function

Both of these are on the fly and my ASP is getting rustier, but the concepts should be solid,

-Tarwn

[sub]01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111[/sub]
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top