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

Elasped Time

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.