|
-
Nov 3rd, 2007, 10:03 AM
#1
Thread Starter
Fanatic Member
-
Nov 3rd, 2007, 10:17 AM
#2
Frenzied Member
Re: Tricky Format Question ???
You could try converting the caption to a string first - this may work.
-
Nov 3rd, 2007, 10:30 AM
#3
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Now I have
Code:
Option Explicit
Dim TimeS As String
Private Sub Command1_Click()
TimeS = TimeS + 0.01
Label1.Caption = Format(TimeS, "#######00:00:00")
End Sub
Private Sub Form_Load()
TimeS = 0
End Sub
But the format does not work label1 remains at 00:00:00 no matter how many times I click command1, that is whatever the value of TimeS is makes no difference
-
Nov 3rd, 2007, 10:42 AM
#4
Frenzied Member
Re: Tricky Format Question ???
You could try converting the caption to a string first - this may work.
Caption is String?? 
@JohnSavage
This double value is a value you created right? So I dont think VB will be able to format it to your Format. Try this
Code:
Public Function FormatTime(TimeVal As Double) As String
Dim TimeString As String
Dim Mins As String
Dim Secs As String
Dim Fractions As String
Mins = CStr(TimeVal \ 60)
Secs = CStr(Int(TimeVal) Mod 60)
Fractions = Mid$(CStr(TimeVal), InStr(1, CStr(TimeVal), ".") + 1)
TimeString = Format(Mins, "0#") & ":" & Format(Secs, "0#") & ":" & Format(Fractions, "0#")
FormatTime = TimeString
End Function
This is not complete. Add the validatins.
-
Nov 3rd, 2007, 10:43 AM
#5
Frenzied Member
Re: Tricky Format Question ???
You said you have double value. Then what is this?
Dim TimeS As String
-
Nov 3rd, 2007, 10:50 AM
#6
Frenzied Member
Re: Tricky Format Question ???
 Originally Posted by zeezee
You said you have double value. Then what is this?
Dim TimeS As String

That was because of what I suggested 
I misread the question, sorry.
-
Nov 3rd, 2007, 10:54 AM
#7
-
Nov 3rd, 2007, 10:58 AM
#8
Re: Tricky Format Question ???
Here's one way
Code:
Const TEST_DATA As Double = 105.23
Dim Hours As Integer
Dim Minutes As Integer
Minutes = TEST_DATA Mod 60
Hours = (Int(TEST_DATA) - Minutes) / 60
MsgBox Hours & ":" & Minutes & ":" & Format((TEST_DATA - Int(TEST_DATA)) * 100, "0#")
-
Nov 3rd, 2007, 11:04 AM
#9
Frenzied Member
Re: Tricky Format Question ???
Martin. I think its same as the one I gave in Post #4??
BTW, the problem here is his Double val has Minites, Seconds and 100th of Seconds. No Hours.

EDIT: ON second Thought Its a better one to my solution. So Op could make it a function ?
-
Nov 3rd, 2007, 11:07 AM
#10
Re: Tricky Format Question ???
 Originally Posted by zeezee
Martin. I think its same as the one I gave in Post #4??
BTW, the problem here is his Double val has Minites, Seconds and 100th of Seconds. No Hours.

EDIT: ON second Thought Its a better one to my solution. So Op could make it a function ?
Sorry I didn't notice your post, but why do you say there is a problem with hours when the minutes are fairly easily converted into hours?
-
Nov 3rd, 2007, 11:10 AM
#11
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
-
Nov 3rd, 2007, 11:12 AM
#12
Re: Tricky Format Question ???
-
Nov 3rd, 2007, 11:29 AM
#13
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Martin
Yes yours worked, but forgive me it will take me a while to test it in the simple app I have going
Thanks
-
Nov 3rd, 2007, 11:44 AM
#14
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Now I have tested it, it almost works the problem is still in the formatting
1:9:09 should be formatted as 01:09:09
Is there a simple way to get this , not that anything is ever simple in VB to me
ps I have noticed also that a well formatted number all of a sudden changes to 8 decimal places why does this happen as it is a problem I have had before
-
Nov 3rd, 2007, 11:48 AM
#15
Re: Tricky Format Question ???
Sure.
Code:
Const TEST_DATA As Double = 105.23
Dim Hours As Integer
Dim Minutes As Integer
Minutes = TEST_DATA Mod 60
Hours = (Int(TEST_DATA) - Minutes) / 60
MsgBox Format(Hours, "0#") & ":" _
& Format(Minutes, "0#") & ":" _
& Format((TEST_DATA - Int(TEST_DATA)) * 100, "0#")
-
Nov 3rd, 2007, 12:45 PM
#16
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Still battling on, this is what I have now
Code:
Option Explicit
Dim TimeVal As Double
Private Sub Form_Load()
TimeVal = 30
End Sub
Private Sub ccrpUpDown1_DownClick()
TimeVal = TimeVal - 0.01
Call Display
End Sub
Private Sub ccrpUpDown1_UpClick()
TimeVal = TimeVal + 0.01
Call Display
End Sub
Private Sub Display()
Dim Hours As Integer
Dim Minutes As Integer
Minutes = TimeVal Mod 60
Hours = (Int(TimeVal) - Minutes) / 60
Label1.Caption = Format(Hours, "0#") & ":" & Format(Minutes, "0#") & ":" & Format((TimeVal - Int(TimeVal)) * 100, "0#")
Label2.Caption = TimeVal
End Sub
As you can see I have added a spinbutton to make changing the value quicker but the methods are the same. I have also added another label (label2) so I can see what is going on.
When the TimeVal = 30.49 then label1 has 00:30:49 which is perfect
But when TimeVal =30.5 then label1 has 00:31:50 which does not make any sense at all
-
Nov 3rd, 2007, 01:04 PM
#17
Re: Tricky Format Question ???
I'm confused. You say that 30.49 means 30 minutes and 49 seconds, but then you say that 30.5 isn't 30 minutes 50 seconds. What does the decimal portion represent?
-
Nov 3rd, 2007, 01:12 PM
#18
Re: Tricky Format Question ???
The decimal portion is in 100ths of a minute correct? But the display portion is in 60ths of a minute corrrect? I think your calcs for the seconds are off. Try this instead:
Format(60*(TimeVal - Int(TimeVal)) , "0#")
 Originally Posted by John
When the TimeVal = 30.49 then label1 has 00:30:49 which is perfect
P.S. If I am interpretting correctly, then the 30:49 is not perfect because .49 is not 49 seconds, it is closer to 30 seconds, 1/2 a minute.
Last edited by LaVolpe; Nov 3rd, 2007 at 01:16 PM.
-
Nov 3rd, 2007, 01:16 PM
#19
Re: Tricky Format Question ???
If what LaVolpe says is true then you just need to make a small change to my code.
Code:
Const TEST_DATA As Double = 30.5
Dim Hours As Integer
Dim Minutes As Integer
Minutes = TEST_DATA Mod 60
MsgBox Format(Hours, "0#") & ":" _
& Format(Minutes, "0#") & ":" _
& Format((TEST_DATA - Int(TEST_DATA)) * 60, "0#")
-
Nov 3rd, 2007, 01:28 PM
#20
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Martin
Changing to 60 just makes more of a problem
I am trying to format a number of seconds into minutes / seconds and tenths of a second
so 90.03 should come out as 01:30:03
-
Nov 3rd, 2007, 01:29 PM
#21
Re: Tricky Format Question ???
-
Nov 3rd, 2007, 01:33 PM
#22
Re: Tricky Format Question ???
And what would 90.75 be? Sounds like it should be 1:30:45
-
Nov 3rd, 2007, 01:42 PM
#23
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
No that would be
01:30:75
as the original (90.75) number is also in seconds
-
Nov 3rd, 2007, 01:44 PM
#24
Re: Tricky Format Question ???
So why isn't 30.5 ==> 00:31:50
-
Nov 3rd, 2007, 01:53 PM
#25
Re: Tricky Format Question ???
I'm sure Martinliss meant 00:30:50
If 90.75 is not 1:30:45 and 90.03 is 1:30:03 then 30.5 must be 00:30:50, likewise, 30.05 would be 30:05
I think both of us are confused. The decimal portion, what is the max range? Is it 0 to 99? If so is the display suppose to be .00 to .99?
Edited: you mentioned 10ths of a second in one of your posts? Just to be sure, you are not trying to display something like the following, are you?
hh : mm : ss : tt where tt is tenths.
Last edited by LaVolpe; Nov 3rd, 2007 at 02:00 PM.
-
Nov 3rd, 2007, 02:14 PM
#26
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Lets start again the number I want to format is seconds.
I want to format it into minutes and seconds but also display it’s decimal element to 2 decimal places therefore
5.6 would be 00:05:60
5.06 would be 00:05:06
60.01 would be 01:00:01
75.09 would be 01:15:09
130.75 would be 02:10:75
Or am I going senile
-
Nov 3rd, 2007, 02:26 PM
#27
Re: Tricky Format Question ???
No you are not going senile and I misread something or you posted something incorrect.
Minutes = TimeVal Mod 60
Hours = (Int(TimeVal) - Minutes) / 60
Label1.Caption = Format(Hours, "0#") & ":" & Format(Minutes, "0#") & ":" & Format((TimeVal - Int(TimeVal)) * 100, "0#")
But when TimeVal =30.5 then label1 has 00:31:50 which does not make any sense at all
When I try that, the value is 00:30:50 not 00:31:50
-
Nov 3rd, 2007, 02:42 PM
#28
Thread Starter
Fanatic Member
Re: Tricky Format Question ???
Here is the code
Code:
Private Sub Display()
Dim Hours As Integer
Dim Minutes As Integer
Minutes = TimeVal Mod 60
Hours = (Int(TimeVal) - Minutes) / 60
Label1.Caption = Format(Hours, "0#") & ":" & Format(Minutes, "0#") & ":" & Format((TimeVal - Int(TimeVal)) * 100, "0#")
Label2.Caption = Format(TimeVal, "#.00")
End Sub
When label2 is 30.49 then it formats label1 to 00:30:49 which is correct
When label2 is 30.50 then it formats label1 to 00:31:50 which is incorrect, label1 should be 00:30:50
-
Nov 3rd, 2007, 02:48 PM
#29
Re: Tricky Format Question ???
Your pc can't add . My label1 shows 00:30:50 and label2 shows 30.50
All I did was copy your exact code to a click event, and add the additional code of: Dim TimeVal As Double & TimeVal = 30.5
Edited. Ok, but if I make TimeVal=31.5, I get 32.5. Rounding issues when dividing. Calculate minutes differently: Minutes = Int(TimeVal) Mod 60
Last edited by LaVolpe; Nov 3rd, 2007 at 03:20 PM.
-
Nov 3rd, 2007, 03:10 PM
#30
Re: Tricky Format Question ???
I think you may be making this more difficult than you need to. All you have to do is pass the integer portion of the double to TimeSerial to cast it as a date. That part of the format is easy. The other part is to scale the float by 100 so you always get 2 digits when you format as a number:
Code:
Private Sub Test()
Debug.Print CustomFormat(130.5)
End Sub
Private Function CustomFormat(dTime As Double) As String
Dim d100ths As Double, dTemp As Date
d100ths = (dTime - Int(dTime)) * 100 'Calculate 100ths and scale by 10^2.
dTemp = TimeSerial(0, 0, Int(dTime)) 'Convert to a date type.
CustomFormat = Format$(dTemp, "nn:ss:") & Format$(d100ths, "##") 'Return formatted.
End Function
-
Nov 3rd, 2007, 04:54 PM
#31
Thread Starter
Fanatic Member
-
Nov 3rd, 2007, 06:37 PM
#32
Re: Tricky Format Question ???
 Originally Posted by JohnSavage
Comintern I am am sure your solution is correct but as I have no idea how to fit it into the code above I will stick with LaVolpe's solution - but thanks for your input 
Just convert it to a one-liner (no need for any other code).
Code:
Label1.Caption = Format$(TimeSerial(0, 0, Int(TimeVal)), "nn:ss:") & _
Format$((TimeVal - Int(TimeVal)) * 100, "##")
-
Nov 3rd, 2007, 08:10 PM
#33
Thread Starter
Fanatic Member
Re: [RESOLVED] Tricky Format Question ???
-
Nov 4th, 2007, 04:02 AM
#34
Thread Starter
Fanatic Member
Re: [RESOLVED] Tricky Format Question ???
Comintern
Just got around to trying your solution this morning.
It does not work, it's OK for say 30.73 which it formats to 00:30:73
But 30.09 it formats to 00:30:9 and 30.00 it would format to 00:30:
But thanks anyway!!
-
Nov 4th, 2007, 08:49 AM
#35
Re: [RESOLVED] Tricky Format Question ???
My bad. It the same thing Martin did in his first shot. It should be:
Code:
Label1.Caption = Format$(TimeSerial(0, 0, Int(TimeVal)), "nn:ss:") & _
Format$((TimeVal - Int(TimeVal)) * 100, "0#")
-
Nov 4th, 2007, 09:43 AM
#36
Thread Starter
Fanatic Member
Re: [RESOLVED] Tricky Format Question ???
Brilliant - one line instead of 5 now if I could code like that all my app's would be less than 1mb
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|