Results 1 to 4 of 4

Thread: [RESOLVED] Formatting decimals

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Resolved [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.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Smile 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

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2008
    Posts
    38

    Re: Formatting decimals

    Quote 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
  •  



Click Here to Expand Forum to Full Width