Basic Math Question

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
This should be a very basic question for almost everyone here, but since I dont know alot about vb.net yet, i dont know the answer.

In a buttons code I want it to add the value of what is in TextBoxes and display it in a label.


I want it to add then divide the following by 4:

AttackBox+DefenseBox+StrengthBox+HitsBox/4
 
Last edited by a moderator:
NEVERMIND about that above part, i have figured it out, but now i need to know the code to have it round the number
 
Not sure it is what you want?

label.text = (AttackBox.text + DefenseBox. text + StrengthBox.text + HitsBox.text) / 4
 
Well I have figured out how to do the regular math stuff.

Could you use the Math.Round funcation in an example for me?
 
I tried

Label14.Text = Math.Round.Label16


But it says that round failed. How would I use the round function to round whats in Label16 and display it in Label14?
 
I searched and searched and still cant get the error to go away, keeps on saying that
Label14 = Math.Round(Label16.Text, 0)
is wrong.
 
Try Label14 = Math.Round(cint(Label16.Text), 0)



*Post Edit


Label14 = cint(Label16.Text)

If label16 is 45.3, then cint(45.3) would give 45 as integers are whole numbers anyway.

Just tried it and CInt() rounds a decimal to a whole number so Math.Round is not needed.
 
Last edited by a moderator:
this is crazy.. it says Value of type Integer cannot be converted to System.Windows.Forms.Label.

it underlines CInt(Label16.Text) in blue and says that above error... any ideas?
 
Last edited by a moderator:
Put up the segment of code, not just the one line but the entire section where you are trying to populate the label
 
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error Resume Next
Assign text box values to variables
Attack = AttackBox.Text
Defense = DefenseBox.Text
Strength = StrengthBox.Text
Hits = HitsBox.Text
Ranged = RangedBox.Text
Magic = MagicBox.Text
Prayer = PrayerBox.Text

Calculates Combat Level
If (Ranged * 1.5) < (Attack + Strength) Then
Label16.Text = (Attack + Defense + Strength + Hits) / 4 + (Magic + Prayer) / 8
End If
Calculates Combat Level
If (Ranged * 1.5) > (Attack + Strength) Then
Label16.Text = (Defense + Hits) / 4 + (Magic + Prayer) / 8 + Ranged * 0.375
End If
Calculates how many lvls needed to advance
Label21.Text = Label16.Text / 4 / 8
Label22.Text = Label16.Text / 8 / 2
Label14 = CInt(Label16.Text)
End Sub
 
An obvious mistake I found!!, But not sure if you just left it out in the post.

You have:
Label14 = Math.Round(Label16.Text, 0)

Change to
Label14.Text = Math.Round(Label16.Text, 0)
 
i feel pretty stupid.. lol, considering i almost always try adding ".text" when stuff doesnt work right..

now i just have one more little question.. is it possible to make it round .5 and lower down instead of rounding the .5 up?

like if its 71.5 it would round it down to 70.

thanks for your help
 
Label14.Text = Math.Round(CDbl(Label16.Text) - 0.01)

May have to change the 0.01 depends on how many decimal places you end up with after your calcs.
 
Originally posted by sjn78
Label14.Text = Math.Round(CDbl(Label16.Text) - 0.01)

May have to change the 0.01 depends on how many decimal places you end up with after your calcs.

Might need Cstr... im not sure...Label14.Text = Cstr(Math.Round(CDbl(Label16.Text) - 0.01))
 
Back
Top