Results 1 to 3 of 3

Thread: [RESOLVED] How to properly format numbers passed from a string? - i.e. 06 instead of 6

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2008
    Posts
    60

    Resolved [RESOLVED] How to properly format numbers passed from a string? - i.e. 06 instead of 6

    I'm trying to learn how to properly format numbers and I don't seem to be having any luck. I wrote a small example program in an attempt to pinpoint exactly where I'm going wrong, but after slowly working my way through the example program I've come to be at a complete loss on where to even attempt inserting the format function.

    In the below example I've got three captions with time format on them. (H:MM:SS:mmm) The goal of the example is to add the three captions so that a fourth caption displays the total time. What I'm getting as a total is "5:40:2:0" where I'm wanting "5:40:02:000".

    From what I've come to understand I need to do something along the lines of:

    Value = Format$(Value, "00000.00")

    However, looking through my example I can't seem to figure out where to even attempt this, nor am I even sure if it's needed. I know that the point that the number gets out of the correct format is when it's passed into the TempValues array. I'm assuming this is because TempValues is an integer and I'm passing the info from a string to an integer. So how do I make this number keep its format?

    Any help figuring this out would be much appreciated. I'm sure it's got to be something simple I'm either overlooking or just don't know.
    Code:
    Option Explicit
    
    Dim Hours As Variant, Minutes As Variant, Seconds As Variant, Milliseconds As Variant
    
    Private Sub CalculateTotal_Click()
    
        'Begin Add the totals
        Calculate (Time1.Caption)
        Calculate (Time2.Caption)
        Calculate (Time3.Caption)
        'End Add the totals
    
        'Begin Edit the totals to account for where they've gone over time format
        
    BeginMilliseconds:
    
        If Milliseconds > 1000 Then
            Milliseconds = Milliseconds - 1000
            Seconds = Seconds + 1
            GoTo BeginMilliseconds
        End If
        
    BeginSeconds:
    
        If Seconds > 60 Then
            Seconds = Seconds - 60
            Minutes = Minutes + 1
            GoTo BeginSeconds
        End If
    
    BeginMinutes:
    
        If Minutes > 60 Then
            Minutes = Minutes - 60
            Hours = Hours + 1
            GoTo BeginMinutes
        End If
        
        'End Edit the totals to account for where they've gone over time format
        
        'Begin Change caption to reflect the total time
        TimeTotal.Caption = Hours & ":" & Minutes & ":" & Seconds & ":" & Milliseconds
        'End Change caption to reflect the total time
    
    End Sub
    
    Private Function Calculate(Time As String)
    
        Dim StringToSplit As String, SplitString() As String
        Dim TempValues(0 To 3) As Integer, h As Integer
        
        'Begin StringToSplit equals the time written in the time caption
        StringToSplit = Time
        'End StringToSplit equals the time written in time caption
        
        'Begin SplitString is an array containing each of the values from the time caption
        SplitString() = Split(StringToSplit, ":")
        'End SplitString is an array containing each of the values from the time caption
        
        'Begin For each of the values in SplitString add it to a temporary array called TempValues
        For h = 0 To UBound(SplitString)
            TempValues(h) = SplitString(h)
        Next
        'End For each of the values in SplitString add it to a temporary array called TempValues
        
        'Begin Add the four split values to hours, minutes, seconds and milliseconds
        Hours = Hours + TempValues(0)
        Minutes = Minutes + TempValues(1)
        Seconds = Seconds + TempValues(2)
        Milliseconds = Milliseconds + TempValues(3)
        'End Add the four split values to hours, minutes, seconds and milliseconds
    
    End Function
    
    Private Sub Exit_Click()
    
        Unload Me
    
    End Sub
    
    Private Sub Form_Load()
    
        'Begin Set the captions to times
        Time1.Caption = "01:22:02:000"
        Time2.Caption = "02:40:00:000"
        Time3.Caption = "01:38:00:000"
        TimeTotal.Caption = ""
        'End Set the captions to times
    
    End Sub
    Last edited by InfusedEMP; Oct 14th, 2011 at 10:04 PM. Reason: Had a couple of un-needed variables in the example

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: How to properly format numbers passed from a string? - i.e. 06 instead of 6

    TimeTotal.Caption = Format(Hours, "0") & ":" & Format(Minutes, "00") & ":" & Format(Seconds, "00") & ":" & Format(Milliseconds, "000")

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2008
    Posts
    60

    Re: [RESOLVED] How to properly format numbers passed from a string? - i.e. 06 instead

    Wow that turned out to be simple. It never even crossed my mind to apply the format to the caption itself, I figured the extra 0's were lost long before the code got to that point. Thanks a lot!

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