Thursday, September 25, 2008

Sample Program Visual Basic - Calculation Program


  • The hands on example illustrates variables, the Val function, and the FormatCurrency function.
  • Three text boxes, seven labels, and four command buttons comprise the solution
  • Be aware that there is a problem with the format command in general. The problem is that the discount amount is visually rounded by the FormatCurrency function to display in the “15% Discount” label. However, the calculation of the discount can create an answer that contains three or four decimal places. When the discount is subtracted from the Extended price, the answer can appear to be incorrect. Convince yourself. Try this: 10 units at $39.95. The answer displayed for Extended price is $399.50, which is correct. The answer displayed for the discount is $59.93 (rounded up from the actual answer of 59.925). The amount displayed for the Discounted Price is $339.58, which is the rounded result of 399.50 – 59.925. The solution—without using the Round function because it has not been introduced yet—is to add 5/10ths of a penny (.005) to the discount. Then both the discount and the resulting discounted price will both appear to be correct and will be correct!


Program Source Code

Dim quan, price, exprice, discount As String
Private Sub Calculate_Click()
exprice = quan * price
Text4.Text = FormatCurrency(exprice)
Text5.Text = exprice * discount
Text6.Text = Text4.Text - Text5.Text
Text4.Text = FormatCurrency(Text4.Text)
Text5.Text = FormatCurrency(Text5.Text)
Text6.Text = FormatCurrency(Text6.Text)
End Sub

Private Sub Command1_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Form_Load()
quan = 0
price = 0
exprice = 0
discount = 0.15
Text1.Text = n_quan
Text3.Text = n_price
Text4.Text = n_exprice
End Sub

Private Sub Label9_Click()
End Sub

Private Sub Text1_Change()
quan = Val(Text1.Text)
End Sub

Private Sub Text3_Change()
price = Val(Text3.Text)
End Sub

1 comment:

princessmalou said...

helo sir can you make me a program that calculates decimal,binary,and hexa,with the basic operation that uses assembly language..plz. i really need it. thnank you