Results 1 to 17 of 17

Thread: Math question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849
    I got a number which is in seconds.
    I'd like to translate that to minutes and seconds.
    Like this: ##:##
    Any suggestions?
    Dekel C.

  2. #2
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Yep, here's the code:

    Code:
      Dim Secs As Long
      '...
      'Secs = number of seconds
      Debug.Print Format$(Secs\3600, "00") & ":" & Format$((Secs Mod 3600)\60, "00")

  3. #3
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Oops, sorry, this was only in hours and minutes displayed.

    Correction:

    Code:
    Dim Secs As Long
      '...
      'Secs = number of seconds
      Debug.Print Format$(Secs\3600, "00") & ":" & Format$((Secs Mod 3600)\60, "00") & Format$((Secs Mod 3600) Mod 60, "00")

  4. #4
    Guest

    or like this

    Code:
    Option Explicit
    
    
    Private Sub Command1_Click()
        
        Dim ltime
    
        ltime = Text1.Text
        ltime = ltime / 60
        ltime = Format(ltime, "#0.00")
        Debug.Print ltime & " minutes"
        
    End Sub

  5. #5
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    I don't think that's what he wanted, Larryn.

  6. #6
    Guest
    I got a number which is in seconds.
    I'd like to translate that to minutes and seconds.
    Like this: ##:##
    Any suggestions?
    madc:

    i think ##:## is what my suggestion was...and does...

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849
    What is this Debug.Print?
    Dekel C.

  8. #8
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230
    Debug.Print prints to the Immediate window while in design mode. It can be found under the View menu. A useful tool for debugging.

    shawn
    Shawn Hull
    VB6, SP3 (Professional Edition)

  9. #9
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197
    If you have x seconds, then the number of whole minutes is:
    int(x/60). The number of seconds left is x-int(x/60).

    Is that the same as what the others wrote?
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

  10. #10
    Guest
    That's correct

  11. #11
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Remarks:

    * int(x/60) is the same as x\60 (shorter notation)
    * 1 is a prime, not correct!
    Suppose we have 05:15:23 (as being hh:mm:ss)
    That means 5*3600 + 15*60 + 23 = 18923 seconds
    Let's say that x=18923
    Then, x\60 is the number of minutes: 315.383333333333 (?)
    Solution:
    x\3600 is the number of hours: 5
    x mod 3600 is the number of remaining seconds: 923
    923\60 is the number of real minutes: 15
    923 mod 60 is the number of remaining seconds: 23
    That means 5 hours, 15 minutes and 23 seconds.

    If you say x - (x\60), then 18923 - (18923\60) = 18608 seconds left (?) Hm, what's the use of this?

    And a2427, Debug.Print is also useful while in Run Mode, not only Design Mode. With Ctrl+Break, one can interrupt the program and switch to this window by Ctrl+G.

  12. #12
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197
    Sorry Mad Compie! I missed something out of my formula.

    dekelc didn't say anything about hours, but what you've done is right.

    This is what dekelc wanted (I hope!):

    Say you have x seconds. What is x in terms of minutes and seconds?

    Number of minutes = int(x/60) = m, say
    Number of seconds = x - 60 * int(x/60) = s, say
    (I missed the "60 *" bit before. Sorry!)

    Hence, m:s is the answer.

    Example:

    125 seconds is clearly 2 mins and 5 seconds (2:05).

    Proof:

    x = 125.
    m = int(x/60) = int(2.08....) = 2
    s = x - 60 * int(x/60) = 125 - 60 * 2 = 125 - 120 = 5

    => m:s = 2:5, as required.

    My way works also, but is not as compact as yours.

    Notes:

    * I didn't realise x\60 = int(x/60). I'll remember that! Thanks - it's a good short cut.

    * I know 1 is not a prime! Check out the link in my signature for more details. (I've got a masters in Mathematical Physics!)

    I didn't know VB did modulo division. I assume this is what mod is in this case, right? So, for instance, 11 mod 5 would be 1, right?

    OK. Hope that clears everything up. Sorry dekelc if I confused you.

    Cheers guys,

    Steve
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

  13. #13
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Yes indeed, the Mod operator gives you the remainder of a division (integer or single/double).
    Remark that when using Mod with decimal numbers, the remainder is rounded above or equal to .5
    In C, the % operator equals the VB Mod operator, but is only applied to whole numbers...

  14. #14
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Sorry, but I am bored on a Sunday night...

    And this thread is sufficiently old and contained sufficent pedanticism (heh, that's probably not even a word ), to prompt me to add my 5 cents worth.

    If x is always an integer, integer, then
    Code:
    Dim x As Integer
    x = 18923 
    Debug.Print Format(TimeSerial(0, 0, x), "hh:mm:ss")
    works fine. If x is long then:
    Code:
    Dim x As Long
    Dim h As Integer
    x = 1892300 
    h = x \ 32400 ' 9 hours expressed in seconds
    Debug.Print Format(TimeSerial(h, 0, x Mod 32400), "hh:mm:ss")
    Of course, this code doesn't teach anyone about how to tell time or how to count in base 12, 24 or 60. It just uses the good old functions VB has built in to do the "hard" work for us

    If delekc actually wants mm:ss (where mm represent the number of minutes and not the minutes within an hour, then:

    Code:
    Dim x As Long
    Dim h As Integer
    x = 18923
    h = x \ 32400 ' 9 hours expressed in seconds
    Dim d As Date
    d = TimeSerial(h, 0, x Mod 32400)
    Debug.Print Format(Hour(d) * 60 + Minute(d) & ":" & Second(d), "00:00")
    Cheers


    Paul Lewis

  15. #15
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197

    Thanks Paul!

    Paul,

    Thanks for the reply! I like your sense of humour - I guess this thread did need livening up a bit!

    It's amazing all the functions VB has built in. It makes me wonder how many lines of code I have done it he past that could be replaced by a single function call.

    Take it easy,

    Steve.

    P.S. I looked up pedanticism (oh, the irony!) - doesn't look good I'm affraid!
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

  16. #16
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Talking Do you know what a greater Irony is?

    The fact that the code I gave still only works correctly for periods less than 24hours

    Here is some more code to try to dig myself out of the hole I have ended up in...

    Code:
    Dim x As Long
    Dim hours As Integer
    Dim minutes As Integer
    Dim seconds As Integer
    Dim days As Integer
    Dim d As Date
    x = 77700000
                 
    hours = x \ 32400 ' 9 hours expressed in seconds
    
    d = TimeSerial(hours, 0, x Mod 32400)
    
    days = Int(d) ' because Date type stores dates as Number of days since a given date
    seconds = Second(d)
    minutes = Minute(d)
    hours = Hour(d) + days * 24 ' except that there aren't 24 hours in a day... close enough I hope
     
    Debug.Print Format(hours & ":" & minutes & ":" & seconds, "00:00:00")
    Paul Lewis

  17. #17
    Addicted Member
    Join Date
    Aug 2000
    Location
    Nottingham, England
    Posts
    197

    Fine!

    Paul,

    Hmm, how ironic. I think we should stop this thread at once - it's becoming far too silly! I think we have proved the point.

    I bet dekelc has stopped checking this thread now. A little more than he/she wanted, I think!

    Take it easy (if you have the time...)

    Steve.
    Sent by: Steve Barker
    E-mail: [email protected]

    P.S. I KNOW 1 is not a prime!
    See this thread: http://forums.vb-world.net/showthread.php?threadid=26485

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