728x90 AdSpace

Latest Article

Format$ functions in VB with Example

Format functions

The visual basic function Format$ is used to display numbers and dates in familiar forms and to right justify them.

1.      To see how the Format$ function works, create a form with a command button and a picture box. In the click event of the command button write down the following code:
Picture1.Print Format$(12345.628, “Standard”)

Picture1.Print Format$(12345.628, “Currency”)

Picture1.Print Format$(12345.628, “#,0”)

Picture1.Print Format$(12345.628, “Percent”)

Picture1.Print Format$(12345.628, “Scientific”)

Picture1.Print Format$(“25/9/04”, “Long Date”)

Picture1.Print Format$(“25/9/04”, “Medium Date”)

2.      The explanations of the Format$() functions is also given in your book.

3.      Format$ can also be used to right justify numbers or strings. Try the following examples in the same form (with the picture box and command button):

Picture1.Print Format$(1234567890, “@@@@@@@@@@”)

Picture1.Print Format$(123, “@@@@@@@@@@”)

Picture1.Print Format$(1234.56, “@@@@@@@@@@”)

Picture1.Print Format$($1234.567, “@@@@@@@@@@”)

In this example we have 10 “@” symbols, which means we are giving each string the length 10. To increase or decrease the length we simply need to increase or decrease the number of @ symbols.

Format function is also used for alignment of strings and numbers (right justification).

Make a form and display the result of these expressions in picture box

Picture1.Print Format$("Tuition and Fees", "@@@@@@@@@@@@@@@@@");
Picture1.Print Tab(19);
Picture1.Print Format$(33000, "000000")
Picture1.Print Format$("Books", "@@@@@@@@@@@@@@@@@");
Picture1.Print Tab(19);
Picture1.Print Format$(1234, "000000")
Picture1.Print Format$("Hostel", "@@@@@@@@@@@@@@@@@");
Picture1.Print Tab(19);
Picture1.Print Format$(6000, "000000")

Things to Consider

-          Format function can be used with fixed width font like Courier, so that numbers are lined up. Change Font of picture box to Courier to get desired results.
-          Number of occurrences of @ symbol indicates string to appear with the length of these many characters. We can change the count of @ to change the length in which string should appear.
-          Tab(19) ensures that writing starts from 19th column.
-          ; appearing at end of print statement causes result of next print statement to be displayed on same line
A "0" placeholder in your format string will force a character to appear in that position regardless of the value, or in other words, give you leading and/or trailing zeros.
Format$ functions in VB with Example
  • Title : Format$ functions in VB with Example
  • Posted by :
  • Date : 11:41
  • Labels :

  • Blogger Comments
  • Facebook Comments


Post a Comment