[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:
Function ConvertToTime(ByVal intNum As Integer) As String
'Convert an integer to a time string "hh:mm:ss"
Dim h As String
Dim m As String
Dim s As String
Dim x As String
h = CInt(intNum / 3600).ToString 'Calculate hours
m = CInt((intNum Mod 3600) / 60).ToString 'Calculate minutes
s = CInt(((intNum Mod 3600) Mod 60)).ToString 'Calculate seconds
If h.Length = 1 Then h = "0" & h
If m.Length = 1 Then m = "0" & m
If s.Length = 1 Then s = "0" & s
x = h & ":" & m & ":" & s
Return x
End Function
Thanks for taking a look.
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:
Private Function ConvertToTime(ByVal intNum As Integer) As String
Dim h As Integer = intNum \ 3600
Dim m As Integer = ((intNum Mod 3600) \ 60)
Dim s As Integer = (((intNum Mod 3600) Mod 60))
Return String.Format("{0:00}:{1:00}:{2:00}", h, m, s)
End Function
you could also mess around with the TimeSpan object
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
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.
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
Re: Integer to String function - better way?
VB Code:
Dim numSeconds As Integer 'Put number of seconds here.
Dim myTimeString As String = New TimeSpan(0, 0, numSeconds).ToString()
Re: Integer to String function - better way?
Quote:
Originally Posted by jmcilhinney
VB Code:
Dim numSeconds As Integer 'Put number of seconds here.
Dim myTimeString As String = New TimeSpan(0, 0, numSeconds).ToString()
use TimeSpan.FromSeconds() in that case :D
he wanted custom formatting, so I thought he should do it manually
bleh
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.
Re: Integer to String function - better way?
Thanks to all for your suggestions.