locked
Formatting Numeric Output (VB .NET) RRS feed

  • Question

  • Hi There,

    I would like to display elapsed time for a simple VB Program.  I am using VB 2008 (.net).  I would like to display the elapsed time in "mm:ss" format.  I am having problems, though, figuring out how to format the output text string to force each field to occupy two decimal places.

    For example, when I want to display the time two minutes and zero seconds, my string looks like "2:0" instead of "02:00"

    I have been trying to use the String.Format function:

    Dim intMinutes As Integer
    Dim intSeconds As Integer
    Dim strDisplay as String

    intMinutes = 2
    intSeconds = 0
    strDisplay = String.Format(intMinutes.ToString(), "00") + ":" + String.Format(intSeconds.ToString(), "00")

    What's the proper way to format my output?

    Thanks in advance!


     
    Thursday, October 2, 2008 12:05 PM

Answers

  • Hi,

    You were close.

    Note: Use & to join strings and + in a mathematical expression makes code easier to read
    as in

    Dim result As Double
    result = Math.PI + Math.E
    MessageBox.Show(result.ToString)




    Regards,

    John




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim intMinutes As Integer
    Dim intSeconds As Integer
    Dim strDisplay As String

    intMinutes = 2
    intSeconds = 0

    strDisplay = String.Format(intMinutes.ToString("00") & ":" & intSeconds.ToString("00"))MessageBox.Show(strDisplay)

    End Sub


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Thursday, October 2, 2008 12:24 PM
  •         Dim lpsedTime As New Stopwatch 
     
            lpsedTime.Reset() : lpsedTime.Start() 'put this where you want to start timing 
     
            lpsedTime.Stop() 'put this where you want to stop timing 
     
            'elapsed.Elapsed.ToString.Substring(0, 8) is hh:mm:ss 
            Debug.WriteLine(lpsedTime.Elapsed.ToString.Substring(0, 8)) 
            Dim aStr As String 
            'or  
            aStr = String.Format("{0:00}:{1:00}:{2:00}", _ 
                                 lpsedTime.Elapsed.Hours, lpsedTime.Elapsed.Minutes, lpsedTime.Elapsed.Seconds) 
     

    • Proposed as answer by Blackwood Friday, October 3, 2008 2:37 AM
    • Marked as answer by Xingwei Hu Monday, October 6, 2008 4:05 AM
    Thursday, October 2, 2008 7:04 PM

All replies

  • Hi,

    You were close.

    Note: Use & to join strings and + in a mathematical expression makes code easier to read
    as in

    Dim result As Double
    result = Math.PI + Math.E
    MessageBox.Show(result.ToString)




    Regards,

    John




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim intMinutes As Integer
    Dim intSeconds As Integer
    Dim strDisplay As String

    intMinutes = 2
    intSeconds = 0

    strDisplay = String.Format(intMinutes.ToString("00") & ":" & intSeconds.ToString("00"))MessageBox.Show(strDisplay)

    End Sub


    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Thursday, October 2, 2008 12:24 PM
  • Thanks John!
    Thursday, October 2, 2008 12:28 PM
  • cynergyou said:

    Thanks John!


    Hi,

    Please mark my post As Helpful or / and As Answer then. :-)


    Regards,

    John

    For links to VB.Net tutorials see here.>> http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835
    Thursday, October 2, 2008 12:37 PM
  •         Dim lpsedTime As New Stopwatch 
     
            lpsedTime.Reset() : lpsedTime.Start() 'put this where you want to start timing 
     
            lpsedTime.Stop() 'put this where you want to stop timing 
     
            'elapsed.Elapsed.ToString.Substring(0, 8) is hh:mm:ss 
            Debug.WriteLine(lpsedTime.Elapsed.ToString.Substring(0, 8)) 
            Dim aStr As String 
            'or  
            aStr = String.Format("{0:00}:{1:00}:{2:00}", _ 
                                 lpsedTime.Elapsed.Hours, lpsedTime.Elapsed.Minutes, lpsedTime.Elapsed.Seconds) 
     

    • Proposed as answer by Blackwood Friday, October 3, 2008 2:37 AM
    • Marked as answer by Xingwei Hu Monday, October 6, 2008 4:05 AM
    Thursday, October 2, 2008 7:04 PM