Results 1 to 7 of 7

Thread: Preserve Leading Zeros Question

  1. #1
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Preserve Leading Zeros Question

    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"?

    Code:
    dim number
    dim numberm1
    
    number = "00500"
    
    numberm1 = number - 1
    
    response.write numberm1
    Thanks so much

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    Code:
    NumberOfLeadingZeros = 3
    MyNumber = 18
    
    MyNumberWithLeadingZeros = right(string(NumberOfLeadingZeros,"0") & cstr(MyNumber),NumberOfLeadingZeros)
    
    response.write MyNumberWithLeadingZeros

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    response.write "00"
    response.write numberm1
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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).

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    It would be something like:

    Code:
    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.
    Last edited by jscheuer1; 02-01-2009 at 11:24 AM. Reason: add disclaimer
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for the leg up, that really helps my confusion on the situation.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •