How to convert decimal to time?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    How to convert decimal to time?

    Hai all,
    i want to convert decimal to time. for example

    if i have decimal number 09.6 then i need it as 10.00 and also decimal 10.118 as 11.58. How to convert this?
    Thanx in advance
    with Cheers
    Nirmal.
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by nirmalsingh
    Hai all,
    i want to convert decimal to time. for example

    if i have decimal number 09.6 then i need it as 10.00 and also decimal 10.118 as 11.58. How to convert this?
    Thanx in advance
    with Cheers
    Nirmal.
    Why do you need to convert them in the first place?

    Rey Sean

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      While this should be relatively straight forward - it is a very odd way to maintain time. It appears you are keeping minutes separated from seconds with the decimal.
      It would be considered much more appropriate to keep time simply as seconds (if that's all the precision you require). This way you first have to separate the numbers and handle everything on the left differently from everything on the right.
      Worse = you will run into rounding issues and have to do floating point math unless you handle the number as a string.
      No matter how you slice it this will be ugly code someone will eventually ask "who wrote this" with a disparaging tone in their voice...

      How many decimal places need to be maintained? This code assume no more than 3 and that hours are not considered:


      Private Type MinSec
      Mins as Integer
      Secs as Integer
      End Type


      Private Function SingleToMinsSec s(lTime as Single) as MinSec

      Dim lMin As Long
      Dim lSecs As Single
      Dim lHours As Long

      'this just temporarily set the value of lTime for testing
      lTime = 9.115

      'do the math
      lMin = Fix(lTime)
      lSec = Round(lTime - lMin, 3)
      lSecs = Val(Replace(For mat$(lSec), Chr$(46), vbNullString))
      lMin = lMin + (lSecs \ 60)
      lSecs = lSecs Mod 60

      'return the values
      SingleToMinsSec s.Mins = lMin
      SingleToMinsSec s.Secs = lSecs

      End Function

      Please know that there are MUCH better ways to store time!

      Comment

      Working...