|
-
Oct 18th, 2008, 07:41 PM
#1
Thread Starter
Member
[RESOLVED] Formatting decimals
My program asks for the number of times a baseball player has gone to bat and the number of times he has hit.
when it prints the average, how can I remove the 0 before the decimal?
Also, how can I guarantee that the decimal will be three places long?
Thanks,
Levells
With the following code, when i enter 500,267 it results in .534
but when I enter 500,250 it results in 0.5
Code:
Private Sub cmdCompute_Click()
Dim whole As String, bat, hit, avg As Single
whole = InputBox("Enter the number of times at bat and the number of hits, separated by a comma.", "Batting Average")
picbox.Cls
bat = Val(Left(whole, InStr(whole, ",") - 1))
hit = Val(Right(whole, InStr(whole, ",") - 1))
avg = FormatNumber(hit / bat, 3)
picbox.Print "Being at bat"; bat; "times and hitting"; hit; "times gives a batting average of "; Right(avg, Len(avg))
End Sub
I use VB 6. I've been programming since September 2008.
-
Oct 18th, 2008, 07:56 PM
#2
Re: Formatting decimals
Code:
Dim whole As String, bat As Long, hit As Long, avg As Double
bat = Fix(Val(whole))
hit = Fix((Val(whole) - bat) * 1000)
Now bat is the portion before decimal, hit is up to three places long. If there are more numbers, those are ignored.
-
Oct 18th, 2008, 08:01 PM
#3
Re: Formatting decimals
Code:
Private Sub Command1_Click()
Dim Hits As Integer, AtBats As Integer
Hits = 185: AtBats = 456
MsgBox "My batting average is Ted Williams' best: " & Format$(Hits / AtBats, "#.000")
End Sub
Last edited by Code Doc; Oct 19th, 2008 at 08:42 PM.
Doctor Ed
-
Oct 18th, 2008, 08:16 PM
#4
Thread Starter
Member
Re: Formatting decimals
 Originally Posted by Code Doc
Code:
Private Sub Command1_Click()
Dim Hits As Integer, AtBats As Integer
Hits = 185: AtBats = 456
MsgBox "My batting average is Ted Williams' best: " & Format$(Hits / AtBats, "#.000")
End Sub
Thank you very much, it works like a charm.
Levells
I use VB 6. I've been programming since September 2008.
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
|