Results 1 to 9 of 9

Thread: [RESOLVED] Integer to String function - better way?

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] Integer to String function - better way?

    I have made the following small function to convert a number of sconds supplied to it as an integer, and return a string in a "hh:mm:ss" format. Just wondering if there is a better way to do it with some sort of built in 'Format' instruction? Also, not sure if the 'Mod' operator is a framework instruction?
    VB Code:
    1. Function ConvertToTime(ByVal intNum As Integer) As String
    2.  
    3.         'Convert an integer to a time string "hh:mm:ss"
    4.  
    5.         Dim h As String
    6.         Dim m As String
    7.         Dim s As String
    8.  
    9.         Dim x As String
    10.  
    11.         h = CInt(intNum / 3600).ToString                 'Calculate hours
    12.         m = CInt((intNum Mod 3600) / 60).ToString        'Calculate minutes
    13.         s = CInt(((intNum Mod 3600) Mod 60)).ToString    'Calculate seconds
    14.  
    15.         If h.Length = 1 Then h = "0" & h
    16.         If m.Length = 1 Then m = "0" & m
    17.         If s.Length = 1 Then s = "0" & s
    18.  
    19.         x = h & ":" & m & ":" & s
    20.  
    21.         Return x
    22.  
    23.     End Function
    Thanks for taking a look.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Integer to String function - better way?

    I think MOD is a vb operator and so using it is fine (other languages have their own too)

    CINT might be a vb specific function, I cant remember.

    VB Code:
    1. Private Function ConvertToTime(ByVal intNum As Integer) As String
    2.         Dim h As Integer = intNum \ 3600
    3.         Dim m As Integer = ((intNum Mod 3600) \ 60)
    4.         Dim s As Integer = (((intNum Mod 3600) Mod 60))
    5.  
    6.         Return String.Format("{0:00}:{1:00}:{2:00}", h, m, s)
    7.     End Function
    you could also mess around with the TimeSpan object
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Integer to String function - better way?

    Hi,

    Of course you can use the Format function. Look it up in MSDN and it will tell you everything you need to know.

    e.g. MyStr = Format(Now(), "Long Time")


    This does depend somewhat on the format that you wish to pass, but in principle I'm sure you can come up with something.

    zaza

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Integer to String function - better way?

    look at the panel to the left for more format strings
    http://msdn.microsoft.com/library/en...asp?frame=true

    just a note though, if you want to use those formatting strings, you'd need a dateTime object.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Integer to String function - better way?

    This works:
    Code:
    Function ConvertToTime(ByVal intNum As Integer) As String
    dim ts as new timespan(0,0,intum)
    return ts.hours & ":" & ts.minutes & ":" & ts.seconds
    end function
    TPM

    Add yourself to the VBForums Frappr Map!!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Integer to String function - better way?

    VB Code:
    1. Dim numSeconds As Integer 'Put number of seconds here.
    2.         Dim myTimeString As String = New TimeSpan(0, 0, numSeconds).ToString()
    Last edited by jmcilhinney; Jul 26th, 2005 at 08:28 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Integer to String function - better way?

    Quote Originally Posted by jmcilhinney
    VB Code:
    1. Dim numSeconds As Integer 'Put number of seconds here.
    2.         Dim myTimeString As String = New TimeSpan(0, 0, numSeconds).ToString()
    use TimeSpan.FromSeconds() in that case
    he wanted custom formatting, so I thought he should do it manually
    bleh
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Integer to String function - better way?

    Indeed FromSeconds is a better choice again. In this case you can simply use TimeSpan.ToString because it gives the correct format. If you wanted something more specific you would need to provide custom formatting. ToString also returns a days and milliseconds part if needed. As long as the number of seconds is less than 24 hours and has no fractional part, ToString will give the desired format.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Integer to String function - better way?

    Thanks to all for your suggestions.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


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