|
-
Jul 26th, 2005, 03:44 PM
#1
Thread Starter
Fanatic Member
[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.
-
Jul 26th, 2005, 04:00 PM
#2
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
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!!
-
Jul 26th, 2005, 04:01 PM
#3
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
-
Jul 26th, 2005, 04:04 PM
#4
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!!
-
Jul 26th, 2005, 04:11 PM
#5
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!!
-
Jul 26th, 2005, 06:49 PM
#6
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()
Last edited by jmcilhinney; Jul 26th, 2005 at 08:28 PM.
-
Jul 26th, 2005, 09:07 PM
#7
Re: Integer to String function - better way?
 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 
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!!
-
Jul 26th, 2005, 09:31 PM
#8
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.
-
Jul 27th, 2005, 12:43 PM
#9
Thread Starter
Fanatic Member
Re: Integer to String function - better way?
Thanks to all for your suggestions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|