View Full Version : Visual Basic Question
Aperture
Oct 11, 2007, 05:55 PM
I know this is a Mac forum, but I figured someone could help out. I have a form in VB and it is to calculate the total bill for an order of food.
When the user is typing in the cost of the actual food itself, (as soon as the cursor leaves the text box) I need it to round to 2 decimal places. If there is no decimal to begin with, it should add 2 zeros (.00) to the end of the value in the text box.
Any help is greatly appreciated!
EricBrian
Oct 11, 2007, 06:00 PM
Heya,
You need javascript to do that since it runs client side:
Have a look at onBlur:
http://www.w3schools.com/jsref/jsref_onblur.asp
italiano40
Oct 11, 2007, 06:02 PM
you should write this in java, but
you need to find a way to goto each character and to
search for a "." and if you can't find it then add .00 like this in java
for(int i=0; i<number.length();i++){
if(number.indexOf(i)=="."){
}
}
something like this kind:apple:
sord
Oct 11, 2007, 06:15 PM
Heya,
You need javascript to do that since it runs client side:
Have a look at onBlur:
http://www.w3schools.com/jsref/jsref_onblur.asp
Um...he said Visual Basic, not vbscript. VBScript is a web based client language, Visual Basic is a RAD based development tool for the basic language (and JavaScript has nothing to do with Visual Basic)
EDIT: Also, a quick Google search for rounding came up with this: http://www.a1vbcode.com/a1vbcode/vbforums/Topic17458-3-1.aspx
EricBrian
Oct 11, 2007, 06:16 PM
you should write this in java, but
you need to find a way to goto each character and to
search for a "." and if you can't find it then add .00 like this in java
for(int i=0; i<number.length();i++){
if(number.indexOf(i)=="."){
}
}
something like this kind:apple:
When italiano says java, s/he acutally means javascript.
Also, the code s/he provided isn't quite right.
Using her/his thoughts, you can:
bill_value.indexOf('.') to get the location of the period. If there is no period, -1 is returned.
If -1 is returned, then you know that you can just append the '.00'
If the value is greater than -1, then that number greater than -1 tells you that there is a period and where the period is in the string.
EricBrian
Oct 11, 2007, 06:19 PM
Um...he said Visual Basic, not vbscript. VBScript is a web based client language, Visual Basic is a RAD based development tool for the basic language (and JavaScript has nothing to do with Visual Basic)
EDIT: Also, a quick Google search for rounding came up with this: http://www.a1vbcode.com/a1vbcode/vbforums/Topic17458-3-1.aspx
Ah, I assumed he meant a web form. Oops. Sorry. :(
Aperture
Oct 11, 2007, 06:19 PM
Thank you everyone for your help. The problem is, it has to be done in Visual Basic .NET. It was an assignment in my class and I'm having some trouble. (Actually, our assignment was to go out and make this entire application. I have the entire app written per the specifications, except for this part.)
toddburch
Oct 11, 2007, 11:37 PM
Perhaps something here will spark an idea. http://www.developerfusion.co.uk/forums/p/27779/102266/#102266
Todd
kainjow
Oct 12, 2007, 12:33 AM
I'd like to know, what school is teaching VB.NET? :eek:
Carrot007
Oct 12, 2007, 06:58 AM
I belive (It's been a while since i did vb) you can just put the code in the _lostfocus sub on the form class.
Something like:
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
TextBox1.Text = math.Round (TextBox1.Text,2 )
End Sub
or something quite like it with a bit of checking as so on.
Aperture
Oct 12, 2007, 04:16 PM
Thank you everyone! I finally got it working, I used this code:
txtName.Text = FormatNumber (txtName.Text , 2)
'Turned out to be way simpler than I thought.
Kainjow, I know quite a few schools in the area do and it isn't that uncommon. :)
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.