Results 1 to 13 of 13

Thread: how to add "0" to a one char/num ??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Posts
    105

    Cool

    Hello ,

    Can someone tell me how can i add 0 to the left side of a string if it has one char/number only!
    if it has 2 char/num i want it to stay the same?

    example : "6" to "06"
    "A" to "0A"
    but when it is "7A" or 2 other char/num i want it to stay the same.
    (the format("string","00") - only works on number!)

    thanks in advance ([email protected])


  2. #2
    Guest

    str = iif((len(str)=1),"0" & str , str)

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    More efficient:
    Code:
    sString = Right("00" & sValue, 2)

  4. #4
    Guest
    I must prove you wrong on that one Aaron.

    example : "6" to "06"
    "A" to "0A"

    but when it is "7A" or 2 other char/num i want it to stay the same.
    If you use your code, say like:

    Code:
    sValue = "60A"
    sString = Right("00" & sValue, 2)
    Debug.Print sString 'returns 0A, where's the 6?
    Now, if you use the code sarun gave, like:

    Code:
    sValue = "60A"
    sString = IIf((Len(sValue) = 1), "0" & sValue, sValue)
    Debug.Print sString 'returns 60A
    Understand? So your code is not more efficient.
    Sorry to prove you wrong .

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Moyalt did say though:
    if it has 2 char/num i want it to stay the same?
    Indicating that the value would never be more than 2 digits.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Aaron Wrong? Not likely.



    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411
    I think you will find this works in every case.

    Code:
    'Assume x holds your 1 or two character string
    
    Debug.Print Iif(Len(x)<2 ,Format(x,"!0&"), x)
    Cheers

    P.S. I see that in the time I wrote this a debate has started Cool
    Paul Lewis

  8. #8
    Guest
    I guess you are right Aaron, the Right function is faster than the IIf function, at least from what I've heard. But, just keep in mind, that if the string is more than 2 digits, than you can use the first code. Or just change Aaron's code to 3 instead of 2.

    Code:
    sString = Right("00" & sValue, 3)

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Talking Fuel to the Fire

    Matthew,

    Here is a case for you

    sValue = "60A"
    sString = IIf((Len(sValue) = 1), "0" & sValue, sValue)
    Debug.Print sString 'returns 60A
    What if sValue is "". You see, in your previous post, you asked "what if there were three characters"...so it's only fair to scrutinize your example with the counter case of 0 chars

    And I know it was sarun's example, but since you were having a bit of fun poking holes I thought you'd appreciate being poked back...All in fun of course

    Aren't I a bas***rd

    Cheers
    P.S I like the simplicity of Aaron's case over my own as well
    Paul Lewis

  10. #10
    Guest
    Q: What if sValue is "".

    Code:
    sValue = "" 
    If sValue = "" Then Msgbox "Enter a Value!": Exit Sub
    sString = IIf((Len(sValue) = 1), "0" & sValue, sValue) 
    Debug.Print sString
    Quit poking at me!
    Sorry Aaron .

  11. #11
    Guest
    <code>
    if len(str) = 2 then str = "0" & str
    <code>

    why that would not work?

  12. #12
    Guest
    if len(str)= 1 then str = "0" & str

    i wanted to say

  13. #13
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Yaz:
    That dog will hunt!
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

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



Click Here to Expand Forum to Full Width