Results 1 to 5 of 5

Thread: bloody str(int)

  1. #1

    Thread Starter
    Lively Member muzzi's Avatar
    Join Date
    May 2001
    Location
    syd.nsw.au
    Posts
    79

    bloody str(int)

    Hello All,

    This is really bugging me, and it is imperative to the survival of my project that I get it right.

    I have a few numbers:

    000
    001
    ...
    009

    but when i convert them to a string it takes away the zero's at the front.. so my nice numbers turn to this :

    0
    1
    .
    9

    Can someone PLEASE help me out.. is there ANY way around this..

    Thanks..

  2. #2
    MerryVIP
    Guest
    You can't store zeros before a value in any valuetype. But you can have them in string.

    I'm not 100% sure, but you can try something like this:
    Code:
        Value = Format(Str(ConvertedValue), "000")
    As said, I'm not sure if it works like this. I haven't used format for a long time. Other, longer way you could do like this:
    Code:
        Value = Str(ConvertedValue)
        If Len(Value) = 1 Then
            Value = "00" & Value
        Elseif Len(Value) = 2 Then
            Value = "0" & Value
        End If
    The later one atleast works...Hope this helps,

  3. #3

    Thread Starter
    Lively Member muzzi's Avatar
    Join Date
    May 2001
    Location
    syd.nsw.au
    Posts
    79
    Value = Format(Str(ConvertedValue), "000")
    Oh, you da man!

    Thanks heaps.!!.

  4. #4
    actually just to shorten the code if you have a very large number of 0's before the number that you want shown you can use

    Code:
    Private Sub Form_Load()
        Dim int_value As Integer
        Dim str_value As String
        Dim int_count As Integer
        Dim int_zeros As Integer
        
        int_zeros = 5 'this is the number of places
                      'you want to show in the text
    
        int_value = 4
        str_value = Trim(Str(int_value))
        For int_count = 1 To int_zeros - Len(str_value)
            str_value = "0" & str_value
        Next int_count
        
        Text1.Text = str_value
    End Sub
    I used 5 because the max possible value for an integer is 32,767 which is only 5 places ... if you use a double you can increase int_zeros ... you can increase it with an integer too, but it wouldn't make much sense to.
    Matt Bradbury

    An optimist will claim the glass is half full.
    A pessimist that it is half empty.
    I just think the glass is too big.

  5. #5
    MerryVIP
    Guest
    No problem

    (hehe, had to answer to thanks, rare thing to happen)

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