Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aperture

macrumors 68000
Original poster
Mar 19, 2006
1,876
0
PA
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!
 

italiano40

macrumors 65816
Oct 7, 2007
1,080
0
NY
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
Code:
for(int i=0; i<number.length();i++){
if(number.indexOf(i)=="."){

}

}
something like this kind:apple:
 

sord

macrumors 6502
Jun 16, 2004
352
0
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

macrumors 6502a
Jul 30, 2005
656
7
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
Code:
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.
 

Aperture

macrumors 68000
Original poster
Mar 19, 2006
1,876
0
PA
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.)
 

Carrot007

macrumors regular
Sep 18, 2006
209
0
Yorkshire
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

macrumors 68000
Original poster
Mar 19, 2006
1,876
0
PA
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.