Discussions

Friday, July 13, 2012

Sample Program #7

















Let us create a program that will get the average of three(3) numbers and control the decimal place of the result using the format function.


Private Sub cmdave_Click()
  Dim a, b, c As Integer
  Dim d As Currency

  a = Val(txtn1.Text)
  b = Val(txtn2.Text)
  c = Val(txtn3.Text)
 
  d = (a + b + c) / 3
  txtave.Text = "The average is " + Format(Trim(Str(d)), "0.00") 
End Sub

Discussion:

The format string function  varies between 0 and # sign. The 0 forces the number format to display the zero even if the amount of the number does not reach the digit position while the # sign on the other hand will not. The purpose of using the # sign is to limit the number of digit to be displayed. For example, using #.## in the code above will make a display of .5 instead of 0.50, 3. instead of 3.00 and 1.5 instead of 1.50.