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!

Leading zero problem

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
So, I have a function that returns a 4 digit value (in Hex), but if there is a leading zero it seems to drop off of the face of the earth. So, I tried hardcoding a value that has a leading zero and still have the issue, the leading zero disappears. I went with this:

Dim thisnum : thisnum=0475

and then wrote it out and got 475. So, then I used FormatNumber(thisnum,0,-1) and still got 475. I tried cstr(thisnum) and still got 475. I even tried

Dim thisnum : thisnum = cstr(0475)

and it still wrote out 475. And now I'm out of ideas. How in the blazes do I get that leading zero to print?

Thanks,
Willie
 
What about this ?
thisnum=[!]"[/!]0475[!]"[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Nope, same problem. I cannot get it to include the leading zero.
 
Code:
Dim thisnum : thisnum=0475
Will give you decimal 475. If you want it in hex it should be
Code:
Dim thisnum : thisnum=&H0475
If you wish to print 4 digits with a leading zero, you have to mess with it.
Code:
WScript.Echo right ("0000" & hex(thisnum), 4)
 
Nope, same problem
How do you "write" the value ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, my example was just showing a 4 digit example that I could not get to work. My code produces a 4 digit hex number, that wasn't my question. My question was how do I get vbscript to actually write out what it receives. Regardless of decimal or hex, it still drops the leading zero. even if I do a cstr it drops the leading zero. If I use the built-in function that is supposed to have an option to return leading zeros it still doesn't work (and I found somebody's demo code online and it had the same problem). Is there no other way than padding with a bunch of leading zeros and then taking the right four characters? Can anybody get FormatNumber working properly to print leading zeros? Or am I trying to use it incorrectly?
 
What is YOUR actual code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Write it to screen?

<%= FormatNumber(GetIt,0,-1) %>

Write it to the variable?

GetIt = Hex(Temp)

And there I have it. My variable may, in fact, be only 3 Hex characters. But, that still leaves me with the issue of why I cannot get FormatNumber to work...
 
My question was how do I get vbscript to actually write out what it receives.
1) What does your receive code look like? Inputbox, read from stdin or something else.
2) What are you receiving from - file, user input, comms link?
3) What are receiving into? string, number
 
Well, in the example, I was defining a simple variant and assigning it a value upon definition. In the code it is a calculated value (and I realized that it may, in fact, be missing the leading zero that I thought was there). With no subvariant declared, I assumed it would be an implicit number.
 
So, you format a string and then you do calculation with it ?
Your result is no longer a string but a numeric value ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Does Hex(temp) qualify as a calculation? If so, then yes. I do a number of calculations, then end up with a value that I convert to Hex using the Hex function.

GetIt = Hex(temp)

I then write that out using <%= GetIt %>, which didn't give me what I wanted, so I tried <%= FormatNumber(GetIt,0,-1) %> which still didn't give me what I was expecting, so I went with a value that I knew for sure for testing.

Dim thisnum : thisnum = 0475 (and also tried thisnum = "0475")

and then wrote that out using
<%= thisnum %> and <%= FormatNumber(thisnum,0,-1) %>
both of which gave me 475 (instead of the 0475 that I was expecting). Now, am I using this incorrectly? Perhaps that is the basis of my confusion?
 
The Hex function returns a string without padding.
You have to use the workaround suggested by xwb:
GetIt = Right("0000" & Hex(temp), 4)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
So, I got it to work with a little Function where I can define how long I want it to be and it will just add zeros if needed:

Function PadDigits(n, totalDigits)

PadDigits = Right(string(totalDigits,"0") & n, totalDigits)

End Function

<%= PadDigits(GetIt,4) %>

But what about FormatNumber, was I trying to use it incorrectly?

WB
 
There is no way (AFAIK) with the FormatNumber function to do what you want.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Got it. It sure seemed that way as I was playing with it, but I wasn't sure. Thanks for the help folks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top