|
-
Feb 22nd, 2013, 09:18 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Convert minutes to hour ( round of minutes portion )
i am converting the minutes to hours & minutes
for example 1439 minutes = 24:59
i need to round of the minutes to 60 and then add it to hours if the minute is more
than or equal to 60 and than show the minutes portion , i am little tired & confused any help please
my code is as fallows
Code:
Public Function HourConverter(ByVal Ip_value As Single) As String
'' converts the ip value to hours
'' < XML ERR LOG Or Ip_value = "" >
Dim Op As String
Dim hrval As Int16 = Int(Ip_value / 60)
Dim Modval As Single = Ip_value Mod 60
If IsDBNull(Ip_value) Or Ip_value = Nothing Then
Ip_value = 0
End If
Op = hrval & "." & Modval
Return Op
End Function
-
Feb 22nd, 2013, 09:31 AM
#2
Re: Convert minutes to hour ( round of minutes portion )
FYI, 1439 minutes is 23:59. If you don't want to do the computation take a look at TimeSpan.
-
Feb 22nd, 2013, 09:38 AM
#3
Re: Convert minutes to hour ( round of minutes portion )
TimeSpan is the way to go...
Code:
Dim ts As TimeSpan = TimeSpan.FromMinutes(1439)
MessageBox.Show(String.Format("Days: {0}, Hours: {1}, Minutes: {2}", ts.Days, ts.Hours, ts.Minutes))
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 22nd, 2013, 09:42 AM
#4
Re: Convert minutes to hour ( round of minutes portion )
Timespan was my first thought too...
-tg
-
Feb 22nd, 2013, 09:47 AM
#5
Thread Starter
PowerPoster
Re: Convert minutes to hour ( round of minutes portion )
thanks for reply , & fine it is working
please try to round off left over 59 minutes to 60
and now it will be 01 hour
accordingly
23 hrs + 1 hr = 24 hr
i mean rounding off the last digit ( that is 9 to 10)
if the last digit is >= 5 then round it off to 10
some thing like this !
-
Feb 22nd, 2013, 10:00 AM
#6
Re: Convert minutes to hour ( round of minutes portion )
What if the minutes part is 49? Would it get rounded to 50? What are you trying to do?
-
Feb 22nd, 2013, 10:04 AM
#7
Thread Starter
PowerPoster
Re: Convert minutes to hour ( round of minutes portion )
yes 45 to 49 is 50
and < 45 is 40
-
Feb 22nd, 2013, 10:20 AM
#8
Hyperactive Member
Re: Convert minutes to hour ( round of minutes portion )
I don't have time to write the code right now, but you could divide your minutes by 10, add 0.5 to the results, round that down to the integer and then multiple that result by 10.
-
Feb 22nd, 2013, 10:20 AM
#9
Re: Convert minutes to hour ( round of minutes portion )
You can easily accomplish that by rounding to the nearest 10 before you calculate hours and minutes.
vb.net Code:
Private Function HourConverter(Ip_value As Single) As String
Ip_value = CInt(Math.Round(Ip_value / 10.0)) * 10
Dim hours = Ip_value \ 60
Dim minutes = Ip_value Mod 60
Return String.Format("{0}.{1}", hours, minutes)
End Function
I would caution against the format that you're returning hours and minutes in though. 1 hour and 20 minutes will be returned as 1.2 which I would interpret to 1 hour and 12 minutes.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Feb 22nd, 2013, 10:28 AM
#10
Hyperactive Member
Re: Convert minutes to hour ( round of minutes portion )
Yes, you would to multiply the remainder by 60 first to obtain the minutes.
-
Feb 22nd, 2013, 10:28 AM
#11
Re: Convert minutes to hour ( round of minutes portion )
Code:
Public Function HourConverter(ByVal Ip_value As Single) As String
'' converts the ip value to hours
'' < XML ERR LOG Or Ip_value = "" >
Dim mins As Integer = CInt(Math.Round(Ip_value / 10.0)) * 10
Dim hrval As Integer = mins \ 60
Dim minval As Integer = mins - (hrval * 60)
Return String.Format("{0}:{1}", hrval.ToString, minval.ToString.PadLeft(2, "0"c))
End Function
-
Feb 22nd, 2013, 10:58 AM
#12
Re: Convert minutes to hour ( round of minutes portion )
here's my contribution w/o using TimeSpan:
Code:
Public Function HourConverter2(ByVal TimeIn As Integer) As String
Dim mins As Integer = TimeIn
' Adjust up/down to the nearest 10 minute mark
mins += IIf((mins Mod 10 < 5), 0 - (mins Mod 10), 10 - (mins Mod 10))
Dim hours As Integer = mins \ 60
mins -= (hours * 60)
Return String.Format("{0}:{1}", hours.ToString, mins.ToString.PadLeft(2, "0"c))
End Function
57 became 1:00
147 became 2:30
and so on...
-tg
-
Feb 22nd, 2013, 11:59 AM
#13
Thread Starter
PowerPoster
Re: Convert minutes to hour ( round of minutes portion )
thanks for one & all
dbsnet & tech posts have helped me perfectly
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
|