473,473 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Elasped Time

WJM
I'm looking for some help with a error in the code below.
When 119.45 seconds are entered the code returns 01:59.45, but when you
enter 119.51 the code returns 02:59.51. But, if I enter 120.00 the code
returns 02:00.00. Can anyone help figure out what is happening?

Public Function Elasped_Time(TimeElapsed As Double) As String
Dim Tenths As Double
Dim Seconds As Double
Dim Minutes As Double
Dim Hours As Double
Dim A As String
'Find The Seconds
Seconds = Int(TimeElapsed) Mod 60
'Find The Minutes
Tenths = TimeElapsed - Int(TimeElapsed)
Minutes = (TimeElapsed \ 60) Mod 60
'Find The Hours
Hours = (TimeElapsed \ 3600)
'Format The Time
If Hours >= 0 Then
A = Format(Hours, "0") & ":"
End If
A = Format(Hours, "00") & ":"
A = A & Format(Minutes, "00") & ":"
A = A & Format(Seconds, "00")
A = A & Format(Tenths, ".00")
Elasped_Time = A
End Function

Nov 19 '06 #1
2 2244
WJM wrote:
I'm looking for some help with a error in the code below.
When 119.45 seconds are entered the code returns 01:59.45, but when you
enter 119.51 the code returns 02:59.51. But, if I enter 120.00 the code
returns 02:00.00. Can anyone help figure out what is happening?

Public Function Elasped_Time(TimeElapsed As Double) As String
Dim Tenths As Double
Dim Seconds As Double
Dim Minutes As Double
Dim Hours As Double
Dim A As String
'Find The Seconds
Seconds = Int(TimeElapsed) Mod 60
'Find The Minutes
Tenths = TimeElapsed - Int(TimeElapsed)
Minutes = (TimeElapsed \ 60) Mod 60
'Find The Hours
Hours = (TimeElapsed \ 3600)
'Format The Time
If Hours >= 0 Then
A = Format(Hours, "0") & ":"
End If
A = Format(Hours, "00") & ":"
A = A & Format(Minutes, "00") & ":"
A = A & Format(Seconds, "00")
A = A & Format(Tenths, ".00")
Elasped_Time = A
End Function
The A97 help file on the '\' operator states:

Before division is performed, the numeric expressions are rounded to
Byte, Integer, or Long expressions.
IMO, the easiest way to fix your current code is always to use the
Int() function prior to using '\' to prevent rounding up by chopping
the decimal part, effectively rendering the rounding ineffective:

Minutes = (Int(TimeElapsed) \ 60) Mod 60

instead of

Minutes = (TimeElapsed \ 60) Mod 60

I tried this out for the situation you gave. It seemed to do the right
thing. Thanks for pointing out this behavior. I have some code to
check.

James A. Fortune
CD********@FortuneJames.com

Jim, you have a natural gift for learning languages. I think I can
have you speaking Mandarin better than I speak English within a year.
By the way, in China there's a complete philosophy centered on tea
preparation. Most of it is grown in the high mountains. There are a
lot of special teas. Why are you laughing? -- Boxiang

Nov 19 '06 #2
CD********@FortuneJames.com wrote:
WJM wrote:
I'm looking for some help with a error in the code below.
When 119.45 seconds are entered the code returns 01:59.45, but when you
enter 119.51 the code returns 02:59.51. But, if I enter 120.00 the code
returns 02:00.00. Can anyone help figure out what is happening?

Public Function Elasped_Time(TimeElapsed As Double) As String
Dim Tenths As Double
Dim Seconds As Double
Dim Minutes As Double
Dim Hours As Double
Dim A As String
'Find The Seconds
Seconds = Int(TimeElapsed) Mod 60
'Find The Minutes
Tenths = TimeElapsed - Int(TimeElapsed)
Minutes = (TimeElapsed \ 60) Mod 60
'Find The Hours
Hours = (TimeElapsed \ 3600)
'Format The Time
If Hours >= 0 Then
A = Format(Hours, "0") & ":"
End If
A = Format(Hours, "00") & ":"
A = A & Format(Minutes, "00") & ":"
A = A & Format(Seconds, "00")
A = A & Format(Tenths, ".00")
Elasped_Time = A
End Function

The A97 help file on the '\' operator states:

Before division is performed, the numeric expressions are rounded to
Byte, Integer, or Long expressions.
IMO, the easiest way to fix your current code is always to use the
Int() function prior to using '\' to prevent rounding up by chopping
the decimal part, effectively rendering the rounding ineffective:

Minutes = (Int(TimeElapsed) \ 60) Mod 60

instead of

Minutes = (TimeElapsed \ 60) Mod 60

I tried this out for the situation you gave. It seemed to do the right
thing. Thanks for pointing out this behavior. I have some code to
check.

James A. Fortune
CD********@FortuneJames.com
Since the decimal values for that example are entered, what I
recommended applies. For the general case where the Double value is
calculated I'd say that a little extra caution is in order.

In:

http://groups.google.com/group/comp....d81c109?hl=en&

I said:

"... it's better to add a little bit of self-defensive margin for
floating point error."

Suppose for argument's sake that TimeElapsed is calculated from a
formula. The self-defensive margin can be realized in that situation
by adding some small value to TimeElapsed before using the Int
function.

Minutes = (Int(TimeElapsed + 0.001) \ 60) Mod 60

59.999 =1
59.9989 =0

Note that the same adjustment would need to be made in the expression
for Seconds so that 59.999 =1 Minute 0 Seconds rather than 1 Minute
59 Seconds.

James A. Fortune
CD********@FortuneJames.com

Nov 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
1
by: davb | last post by:
Trying to sum the overall elasped time in hours using "start" and "end" times in separate fields from multiple rows. I have tried datediff, timediff and datepart functions on this attemp. When...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
4
by: Neo | last post by:
Is there any function in std. C to convert seconds into hour, min and sec. ? Lets say the following code takes 129 seconds, how do i get components of time in "hour min and second" format? Is there...
0
by: Microsoft | last post by:
I'm creating a service and am using a timer in it. When the service starts I enable the timer, and then in the timer elasped event, I'm disabling it (this.srvcTimer.Enabled = false) so the timer...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
2
by: jajalc | last post by:
Hi all, WE have a windows service using .net and which uses a System.Timers.Timer..all the of code works fine..... but after a extended period of time the timer just stops firing the elasped...
1
by: TGrady | last post by:
I'm using MS access. I created a query that calculates elasped time, results are returned in a short time format. I now need to add to the query a way to convert the short time into a decimal......
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.