Formatting Functions
Formatting output is an important part of programming so that the visual interface can be presented clearly to the users. Data in the previous lesson were presented fairly systematically through the use of commas and some of the functions like Int, Fix and Round. However, to better present the output, we can use a number of formatting functions in Visual basic.The three most common formatting functions in VB are Tab, Space, and Format
12.1 The Tab function
The syntax of a Tab function is Tab (n); xThe item x will be displayed at a position that is n spaces from the left border of the output form. There must be a semicolon in between Tab and the items you intend to display (VB will actually do it for you automatically).
Example 12.1
.Private
Sub Form_Activate
Print "I"; Tab(5); "like"; Tab(10); "to"; Tab(15); "learn"; Tab(20);
"VB"
Print
Print Tab(10); "I"; Tab(15); "like"; Tab(20); "to"; Tab(25); "learn";
Tab(20); "VB"
Print
Print Tab(15); "I"; Tab(20); ; "like"; Tab(25); "to"; Tab(30); "learn";
Tab(35); “VB"
End sub
Figure 12.1: Output of Example 12.1
12.2 The Space function
The
Space function is very closely linked to the Tab function. However,
there is a minor difference. While Tab (n) means the item is placed n
spaces from the left border of the screen, the Space function specifies
the number of spaces between two consecutive items. For example, the
procedure
Example 12.2
Private Sub Form_Activate()Print "Visual"; Space(10); "Basic"End Sub
Means that
the words Visual and Basic will be separated by 10 spaces
12.3 The Format function
The Format function is a very powerful
formatting function which can display the numeric values in various
forms. There are two types of Format function, one of them is the
built-in or predefined format while another one can be defined by the
users.
(a) The syntax of the predefined Format function is
Format
(n, “style argument”)
where n is a number and the list of style arguments is
given in Table 12.1
Table 12.1: List of Style Arguments
Style argument |
Explanation |
Example |
---|---|---|
General Number
|
To display the number without having separators between
thousands.
|
Format(8972.234, “General Number”)=8972.234
|
Fixed
|
To display the number without having separators between
thousands and rounds it up to two decimal places.
|
Format(8972.2, “Fixed”)=8972.23
|
Standard
|
To display the number with separators or separators between
thousands and rounds it up to two decimal places.
|
Format(6648972.265, “Standard”)= 6,648,972.27 |
Currency
|
To display the number with the dollar sign in front, has
separators between thousands as well as rounding it up to two
decimal places.
|
Format(6648972.265, “Currency”)= $6,648,972.27
|
Percent
|
Converts the number to the percentage form and displays a % sign
and rounds it up to two decimal places.
|
Format(0.56324, “Percent”)=56.32 %
|
Example 12.3
Private Sub Form_Activate()Print Format (8972.234, "General Number")Print Format (8972.2, "Fixed")Print Format (6648972.265, "Standard")Print Format (6648972.265, "Currency")Print Format (0.56324, "Percent")End Sub
Figure 12.2: Output of Example 12.3
(b) The syntax of the user-defined Format function is
Format (n, “user’s format”)
Although it is known as user-defined format, we still need to follows certain formatting styles.
Examples of user-defined formatting style are listed in Table 12.2
Table 12.2: User-Defined Formatting Functions
Example |
Explanation |
Output |
---|---|---|
Format(781234.57,”0”)
|
Rounds to whole number without separators between thousands.
|
781235
|
Format(781234.57,”0.0”)
|
Rounds to 1 decimal place without separators between thousands.
|
781234.6
|
Format(781234.576,”0.00”)
|
Rounds to 2 decimal places without separators between thousands.
|
781234.58
|
Format(781234.576,”#,##0.00”)
|
Rounds to 2 decimal places with separators between thousands.
|
781,234.58
|
Format(781234.576,”$#,##0.00”)
|
Shows dollar sign and rounds to 2 decimal places with separators between thousands.
|
$781,234.58
|
Format(0.576,”0%”)
|
Converts to percentage form without decimal places.
|
58%
|
Format(0.5768,”0.00%”)
|
Converts to percentage form with 2 decimal places.
|
57.68%
|
Example 12.4
Private Sub Form_Activate()
Print Format(781234.57, "0")End Sub
Print Format(781234.57, "0.0")
Print Format(781234.576, "0.00")
Print Format(781234.576, "#,##0.00")
Print Format(781234.576, "$#,##0.00")
Print Format(0.576, "0%")
Print Format(0.5768, "0.00%")
0 comments:
Post a Comment