rounding problem

  • Thread starter Thread starter Heidi
  • Start date Start date
H

Heidi

I have created a button that when pushed, calculates a
weight based on a length that is entered. I need the
calculated weight that is displayed in the messege box at
the end to be rounded to two decimal places. How do I do
this? Here is the code I used:

Private Sub CalcHalibutWeight_Click()
Dim vntLength As Variant
'Obtain a length of the halibut from the user
vntLength = InputBox("Enter halibut length")
'Call the CalcWeight procedure; pass vntLength as argument
CalcWeight vntLength
End Sub

Sub CalcWeight(vntWeight As Variant)
'Calculate the weight of halibut based on length provided
vntWeight = 0.0003 * vntWeight ^ 3.0589
'Display the weight value calculated
MsgBox "Weight: " & vntWeight
End Sub
 
-----Original Message-----
I have created a button that when pushed, calculates a
weight based on a length that is entered. I need the
calculated weight that is displayed in the messege box at
the end to be rounded to two decimal places. How do I do
this? Here is the code I used:

Private Sub CalcHalibutWeight_Click()
Dim vntLength As Variant
'Obtain a length of the halibut from the user
vntLength = InputBox("Enter halibut length")
'Call the CalcWeight procedure; pass vntLength as argument
CalcWeight vntLength
End Sub

Sub CalcWeight(vntWeight As Variant)
'Calculate the weight of halibut based on length provided
vntWeight = 0.0003 * vntWeight ^ 3.0589
'Display the weight value calculated
MsgBox "Weight: " & vntWeight
End Sub

.

change the second to the last lie of calcWeight to

MsgBox "Weight: " & round(cint(vntweight),2)

Question:

If weight and length are both to be doubles I would
declare them as such rather than variants.

You may want to do som error trapping too. With the input
box it doesn't take much for a user to enter a non-numeric
value which would make this choke.

Try this code, it will allow you to easily calculate the
weight of any fish as long as you pass in the length and
the power to which the length is raised to calculate the
weight. If the .00003 is also a variable for each fish
then you can pass that in too. This also error traps
against not entering numbers. I hope this is helpful.

mweyland at mnqio dot sdps dot org

Private Sub CalcHalibutWeight_Click()
Dim strLength As String
'Obtain a length of the halibut from the user
strLength = InputBox("Enter halibut length")
If (IsNumeric(strLength)) Then
'Call the CalcWeight procedure; pass length and weight
for particular fish per inch
MsgBox "Weight for Halibut is: " & CalcWeight(CInt
(strLength), 3.0589)
Else
MsgBox "This is not a number please enter a number"
CalcHalibutWeight_Click
Exit Sub
End If
End Sub

Private Function CalcWeight(intLength As Integer,
dblWtPerInch As Double) As String
'Calculate the weight of fish based on length provided
CalcWeight = Round((0.0003 * intLength ^ dblWtPerInch), 2)
End Function
 
Back
Top