Results 1 to 13 of 13

Thread: [RESOLVED] Convert minutes to hour ( round of minutes portion )

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Resolved [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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Convert minutes to hour ( round of minutes portion )

    Timespan was my first thought too...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    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 !
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Convert minutes to hour ( round of minutes portion )

    yes 45 to 49 is 50
    and < 45 is 40
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  8. #8
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    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.

  9. #9
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Private Function HourConverter(Ip_value As Single) As String
    2.         Ip_value = CInt(Math.Round(Ip_value / 10.0)) * 10
    3.         Dim hours = Ip_value \ 60
    4.         Dim minutes = Ip_value Mod 60
    5.         Return String.Format("{0}.{1}", hours, minutes)
    6.     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.

  10. #10
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Convert minutes to hour ( round of minutes portion )

    Yes, you would to multiply the remainder by 60 first to obtain the minutes.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Convert minutes to hour ( round of minutes portion )

    thanks for one & all

    dbsnet & tech posts have helped me perfectly
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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