Visual Basic Question

Discussion in 'Mac Programming' started by Aperture, Oct 11, 2007.

  1. Aperture macrumors 68000

    Aperture

    Joined:
    Mar 19, 2006
    Location:
    PA
    #1
    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!
     
  2. EricBrian macrumors 6502a

    EricBrian

    Joined:
    Jul 30, 2005
    #2
  3. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #3
    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:
     
  4. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #4
    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
     
  5. EricBrian macrumors 6502a

    EricBrian

    Joined:
    Jul 30, 2005
    #5
    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.
     
  6. EricBrian macrumors 6502a

    EricBrian

    Joined:
    Jul 30, 2005
    #6
    Ah, I assumed he meant a web form. Oops. Sorry. :(
     
  7. Aperture thread starter macrumors 68000

    Aperture

    Joined:
    Mar 19, 2006
    Location:
    PA
    #7
    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.)
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    I'd like to know, what school is teaching VB.NET? :eek:
     
  9. Carrot007 macrumors regular

    Carrot007

    Joined:
    Sep 18, 2006
    Location:
    Yorkshire
    #10
    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.
     
  10. Aperture thread starter macrumors 68000

    Aperture

    Joined:
    Mar 19, 2006
    Location:
    PA
    #11
    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. :)
     

Share This Page