Log in

View Full Version : Preserve Leading Zeros Question



theflyingminstrel
02-01-2009, 06:21 AM
Hi I need to get the value of a number (-1). If the number has leading zeros they are deleted on output for some reason or another.

Is there a way to preserve any leading zeros that might be present in "number" to be re-outputted in "numberm1"?


dim number
dim numberm1

number = "00500"

numberm1 = number - 1

response.write numberm1

Thanks so much

jscheuer1
02-01-2009, 07:18 AM
I don't think you can, as in any coding language, the number is stripped of all extraneous stuff like that. Once you have your result, you can format it by adding leading zeros, but you would have to change it back into a string to do so. It started out as a string (because it is quoted), but you changed it into a number as soon as you subtracted 1 from it.

theflyingminstrel
02-01-2009, 07:22 AM
Thanks for replying John.

I found this code but I'm just not sure how to use it to automate how many zeros there were to begin with on output.


NumberOfLeadingZeros = 3
MyNumber = 18

MyNumberWithLeadingZeros = right(string(NumberOfLeadingZeros,"0") & cstr(MyNumber),NumberOfLeadingZeros)

response.write MyNumberWithLeadingZeros

jscheuer1
02-01-2009, 07:32 AM
I don't know enough asp or VB off hand to tell you. I could probably look it up. It's tricky though, say your number as a string was "00100" - what would you want the result to be? Would it be:

00099

or:

0099

?

If the second one is acceptable and you know how many leading zeros, you could just do:


response.write "00"
response.write numberm1

theflyingminstrel
02-01-2009, 07:38 AM
Thanks John. Yeah the second one is exactly what I need but the problem is I have absolutely no idea how many leading zeros there might be in the string (or if there would be any at all to begin with).

jscheuer1
02-01-2009, 12:05 PM
It would be something like:


num = "00100"
zeros = ""
do while Left(num, 1) = "0"
num = ltrim(num)
zeros = zeros & "0"
loop
num2 = num - 1
Response.Write(zeroes & num2)

But I doubt that will work as my syntax is wrong, but I hope you get the idea.

theflyingminstrel
02-01-2009, 04:16 PM
Thanks so much for the leg up, that really helps my confusion on the situation.