Results 1 to 6 of 6

Thread: Calculation in Vb.Net

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    55

    Calculation in Vb.Net

    THE PROBLEMS at the bottom of the page, thank you in advance for your time reading this, and i hope you can help btw im new so hello to all of you and i hope in the near future i will be able to help someone as you may to me thank you

    Hello, was wondering if anyone can help me, well my task is to create a programme for a rental company that lends out playstation, dvd and videos. entering a product code such as D13456 and V23456 and P23456 so on ( only 12, 4 of each) and adding the prices up into a text box. I have this code so far.

    Dim counteritem As Integer
    Dim counter As Integer
    Dim myvar As String
    Dim num As Integer
    Dim D As Double
    Dim P As Double
    Dim V As Double
    Dim iprice As Integer
    Dim itotal As Double

    CALCULATION BIT THAT NEEDS SORTING OUT:
    Private Sub cmdcal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcal.Click
    counteritem = lstboxpric.Items.Count - 1
    For counter = 0 To counteritem
    myvar = lstboxpric.Items.Item(Int(counter))

    If myvar = D Then
    num = D
    itotal = itotal + num
    BVAT.Text = "£ " & itotal
    End If
    If myvar = V Then
    num = V
    itotal = itotal + num
    BVAT.Text = "£ " & itotal
    End If
    If myvar = P Then
    num = P
    itotal = itotal + num
    BVAT.Text = "£ " & itotal
    End If
    Next
    itotal = 0
    End Sub

    'This is the prices D = dvd V = video P = Playstation the prices are followed after the equals

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    D = 3.25
    V = 2.55
    P = 1.55
    End Sub

    'This is the values that are added in the list box ready for calculation.

    Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
    lstboxname.Items.Add(lbl1.Text)
    If lbl2.Text = 3.25 Then
    lstboxpric.Items.Add(D)
    ElseIf lbl2.Text = "2.55" Then
    lstboxpric.Items.Add(V)
    ElseIf lbl2.Text = "1.55" Then
    lstboxpric.Items.Add(P)
    End If
    End Sub
    My problem is that all the adding works so that if the 3.25 and 2.55 are in the lstboxpric the BVAT.text would come up with £6 instead of being£5.80 (i no for definate that it is rounding the sums up i have tried so many times) can any one see what i am doing wrong .. thank you so much for reading this i appreciate it .

    I think thats correct so far anyway :/

    much appreciated

    cheers

  2. #2
    Junior Member techmanbd's Avatar
    Join Date
    Dec 2003
    Location
    Studio City, CA
    Posts
    16
    One thing I noticed is when you are putting itotal into a text box, you should be iTotal.tostring because you need to convert the double into a string for the text box since the text box is basically a string.

    Also dim num as double since you are making it d , v or p and they are dimmed as double

    also found this
    If lbl2.Text = 3.25 Then
    you forgot the quotes around 3.25 ---- "3.25"

  3. #3
    Junior Member techmanbd's Avatar
    Join Date
    Dec 2003
    Location
    Studio City, CA
    Posts
    16
    Ok also found something else, You are comparing double with a string


    myvar = d

    can't do that. they need to be of same type. string = string
    or double = double

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    55
    OMG you are ace you just solved it all i been working on that for 3 weeks, come here post my questions ( not very well ) and with an easy to understand answer you came out with thats great

    One last thing

    i just did 3.25 + 3.25 and in the txtbox it came out with £ 6.5 how can i make it so it comes up with £6.50 instead, all the rest works thank you so much

    EDIT:

    just changed myvar to double, although it worked fine before, also noticed like i said before the £6.5 thing it cuts it of all 0's at the end :/

    sorry i have only been into visual basic since september lol

  5. #5
    Junior Member techmanbd's Avatar
    Join Date
    Dec 2003
    Location
    Studio City, CA
    Posts
    16
    ok for the 0 at the end do this

    textbox.text = format(d,"##0.00").tostring


    "##0.00"

    the zeros will always place a number there. the pound sign will put the number there just in case your number is bigger like 10.01. but not put a number if it is smaller like 5.01

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2003
    Posts
    55
    HEY, thanks for the speedy replies, although i cant get the format bit to work, it won't show the 0's

    any chance of an example with my code and the tostring i dnt understand

    cheers buddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width