[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
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.
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))
Re: Convert minutes to hour ( round of minutes portion )
Timespan was my first thought too...
-tg
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 !
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?
Re: Convert minutes to hour ( round of minutes portion )
yes 45 to 49 is 50
and < 45 is 40
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.
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.
Re: Convert minutes to hour ( round of minutes portion )
Yes, you would to multiply the remainder by 60 first to obtain the minutes.
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
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
Re: Convert minutes to hour ( round of minutes portion )
thanks for one & all
dbsnet & tech posts have helped me perfectly