Newbie Question...

Capnbob
02-18-2004, 03:39 PM
I've just started learning Visual Basic and for one of my first projects I'm making a calculator that calculates a person's average time for running a mile.

So far it's working fine but if I put in any non-whole numbers (decimals) then I get a huge number. I'd like to have it only display maybe two or so decimals back. How can I do this?

I may as well copy my code so far....

Private Sub Calculate_Click()
Dim navgmile As Double
navgmile = (Time / Distance)
MsgBox "Your Average Was A " + CStr(navgmile) + " Minute Mile"

End Sub

Private Sub Distance_Change()
Dim Distance As Integer
End Sub

Private Sub Exit_Click()
Unload Me
End Sub

Private Sub Time_Change()
Dim Time As Integer
End Sub


Thanks for any help!

-Capnbob

sseller
02-18-2004, 03:53 PM
Have a look at the Format function in MSDN.

jumentous
02-18-2004, 03:54 PM
For the first thing i hope your using option explicit


You see:
Private Sub Time_Change()
Dim Time As Integer
End Sub
That does nothing, When it runs that it will dim Time as integer however when you dim a variable from within a procedure it is only available from that procedure so you cannot use this in Calculate_Click(), Also an Integer data type can only hold round numbers (ie 1, 2,3,4 etc not 1.4, 1.90 etc) so while that is probably ok for distance (as its over 100m) It is defenitely no good for time

noi_max
02-18-2004, 06:31 PM
maybe something like this



Private Sub Calculate_Click()
Dim navgmile As Double
Dim strAnswer As String
navgmile = (Time / Distance)
strAnswer = Cstr(navgmile)
strAnswer = Format(strAnswer, "#####.#0")
MsgBox "Your Average Was A " & strAnswer & " Minute Mile"



also following up on what jumentous said, I hope the top of your code is lookin like this



Option Explicit
Dim Time As Double
Dim Distance As Double 'etc.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum