By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

How to convert decimal to time?

nirmalsingh
100+
P: 218
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.
Apr 29 '08 #1
Share this Question
Share on Google+
2 Replies


lotus18
100+
P: 866
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
Apr 29 '08 #2

!NoItAll
100+
P: 296
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 SingleToMinsSecs(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(Format$(lSec), Chr$(46), vbNullString))
lMin = lMin + (lSecs \ 60)
lSecs = lSecs Mod 60

'return the values
SingleToMinsSecs.Mins = lMin
SingleToMinsSecs.Secs = lSecs

End Function

Please know that there are MUCH better ways to store time!
Apr 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.