-
Feb 5th, 2023, 04:57 PM
#1
[RESOLVED] Format Function seems to be dropping trailing zeroes in decimal displays
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim startTime As Double
Dim StopTime As Double
Dim getTime As Double
'At the beginning of an event (in my case it gets startTime when I turn on Form KeyPreview
(set to true)at a list_click event), so players can use my handheld (wireless) devices to
answer a question:
startTime = GetTickCount()
Later in my code, when a player DOES use the HH device, I get the StopTime for each player,
so I can later compare and list (in order) their times it took for them to answer the question
and (actually perform some written function 'off line') see how quickly each player does:
StopTime = GetTickCount()
I then immediately calculate getTime (which is number of "seconds" that passed from the
startTime to the StopTime. I use this Format below for an eventual display of those
"seconds" in a 3-decimal view (in a flexgrid column):
getTime = Format((StopTime - startTime) / 1000, "#.000")
I want each time to be displayed in 3 decimal points, which I believe that Format function is
supposed to produce. However, every once in a while, a player's time may end up with two, one,
or even zero decimals. I'm assuming it has something to do with that division (stopTime-startTime / 1000).
I thought using doubles was surely the way to go, but, like I said, sometimes I get numbers like 3.45, 2.4, etc.,
and want 3.450, 2.400, etc. I SUPPOSE I could convert getTime to a String after calculating it,
and add zeroes to pad the display, but that seems like extra work that SHOULDN'T be required.
What am I missing/doing incorrectly?
Last edited by SamOscarBrown; Feb 5th, 2023 at 05:00 PM.
Sam I am (as well as Confused at times).
-
Feb 5th, 2023, 05:11 PM
#2
Re: Format Function seems to be dropping trailing zeroes in decimal displays
Update (no, not solved, but)...
In a separate simple test, this code produced 3334.51 (note: myNumber is dimmed as double)
Code:
Dim myNumber As Double
myNumber = Format("3334.51", "#.000")
Debug.Print myNumber
However, if I dim myNumber as String, it produces 334.510:
Code:
Dim myNumber As String
myNumber = Format("3334.51", "#.000")
Debug.Print myNumber
If the 'solution' is to dim that variable as a String, I can most certainly do so, but it seems strange to do that.
Confused
Sam I am (as well as Confused at times).
-
Feb 5th, 2023, 05:25 PM
#3
Re: Format Function seems to be dropping trailing zeroes in decimal displays
 Originally Posted by SamOscarBrown
If the 'solution' is to dim that variable as a String, I can most certainly do so, but it seems strange to do that.
Confused
Trailing 0's are "numerically" ignored.
Format returns a String, so you shouldn't be storing it in a numeric variable. If all you want to do is display the numeric value with formatting, then you don't need a String variable, just:
-Store the numeric calculation result to an appropriate numeric variable
-Assign the result of your Format command of said numeric variable to the .Text or .Caption property of whatever type of control you are using to display it to the user.
-
Feb 5th, 2023, 05:49 PM
#4
Re: Format Function seems to be dropping trailing zeroes in decimal displays
Yeah, OptionBase1 nailed it. Format() returns a String Variant, and Format$() returns a String (not in a variant). So Format$() is a bit better.
But yeah, Sam, your code is doing tons of implicit casting:
Code:
Dim myNumber As Double
myNumber = Format("3334.51", "#.000")
^ ^ ^
| | | This string is being cast to a number (probably a Double).
| |
| | This is returning a String inside a Variant.
|
| This is casting that String-Variant into a Double so it'll go into myNumber.
And now that it's back into a Double (myNumber), it's again represented as IEEE binary, so it doesn't "perfectly" represent a base-10 number any longer. So, when you print it, you may get a number that's ever so slightly off from what the Format() Variant-String returned.
Personally, the only time I ever use Format$() [and actually never using Format()] is when I'm ready to print/output the number, and not to store it in another variable.
I suppose occasionally, I'll put a Format$() into another string to build a longer string that's got my formatted number in it.
One last thing ... once you've got it into myNumber, any explicit formatting (to a specific number of decimal points) is gone.
Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.
-
Feb 5th, 2023, 06:33 PM
#5
Re: Format Function seems to be dropping trailing zeroes in decimal displays
I see....returns a string...got it (shoulda known that all along...mea culpa).
As far as Format$, got it now as well (also knew that before).
So, I changed getTime to a String. But, now, confused once again...I put the players 'name' and getTime (the total num of seconds) into a String Array (as a matter of a fact, I used CSTR() to put getTime into the array (not even realizing it was already a string). so, I have two dimension array with player and 'seconds'. I then later put those array elements into an MSFlexGrid. I then click on a header on that grid to sort the column (with a sort routine--see below)...and now (realizing that the 2nd element is (and was) astring), I kinda expected it to sort as STRINGS, but it SEEMS to sort as values. ex: 1.234, 4.567, 7.897, 10.332, 12.211, etc...why isn't it sorting by STRING where the 10.332 would come before 4.567, eg.)?
Code:
Private Sub Grid1_Click()
Dim lCol As Long, lRow As Long, i As Integer
If grid1.Text = "" Then Exit Sub 'to prevent error if clicking on a blank line before sorting the grid (which removes blanks)
With grid1
lCol = .MouseCol
lRow = .MouseRow
'~~> If a Fixed Row is clicked
If lRow < .FixedRows And lRow <> -1 Then
'~~> If a Fixed Row is not clicked
If lCol >= .FixedCols Then
.Col = lCol
.Sort = flexSortGenericAscending
End If
Else
txtPlayer(CInt(Mid(grid1.Text, 8, 2)) - 1) = txtPlayer(CInt(Mid(grid1.Text, 8, 2)) - 1) + 1
frmAudience.txtPlayer(CInt(Mid(grid1.Text, 8, 2)) - 1) = frmAudience.txtPlayer(CInt(Mid(grid1.Text, 8, 2)) - 1) + 1
End If
End With
For i = grid1.Rows - 1 To 1 Step -1
grid1.Row = i
If grid1.TextMatrix(i, 1) = "" Then
grid1.RemoveItem (i)
End If
Next i
frmAudience.grid1.Rows = 1
For i = 1 To grid1.Rows - 1
frmAudience.grid1.Rows = frmAudience.grid1.Rows + 1
grid1.Row = i
frmAudience.grid1.TextMatrix(i, 0) = grid1.TextMatrix(i, 0)
frmAudience.grid1.TextMatrix(i, 1) = grid1.TextMatrix(i, 1)
Next i
End Sub
Sam I am (as well as Confused at times).
-
Feb 6th, 2023, 09:16 AM
#6
Re: Format Function seems to be dropping trailing zeroes in decimal displays
Never mind...I (Finally) re-read MSDN on "FlexSortGenericAscending":
Generic Ascending. An ascending sort, which estimates whether text is string or number, is performed.
Sam I am (as well as Confused at times).
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
|