In Visual Basic 2008, we can write code to customize the look of the output so that it can be more easily understood by a user.
The function to customize the output is the Format function , a very powerful function which can display numeric values in various forms. There are two types of Format functions, one of them is the built-in or predefined format while another one can be defined by the users.
(i) The syntax of the predefined Format function is
Format (n,"style argument")
where n is the number to be displayed and style argument is the style of the displayed number .
Style arguments are listed in Table 15.1.
Style argument | Explanation |
---|---|
General Number | To display the number without having separators between thousands. |
Fixed | To display the number without having separators between thousands and rounds it up to two decimal places. |
Standard | To display the number with separators or separators between thousands and rounds it up to two decimal places. |
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. |
Percent | Converts the number to the percentage form and displays a % sign and rounds it up to two decimal places. |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button5.Click, Button4.Click,
Button3.Click
The Output
window is shown in Figure 15.1 below:
Format
(n, "user format")
Label1.Text = Format(8972.234, "General Number")
Label2.Text = Format(8972.2, "Fixed")
Label3.Text = Format(6648972.265, "Standard")
Label4.Text = Format(6648972.265, "Currency")
Label5.Text = Format(0.56324, "Percent")
Figure 15.1
(ii) The syntax of the user-defined Format function is
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 15.2
Format | Description | Output |
---|---|---|
Format(781234.576,"0") | Rounds to whole number without separators between thousands | 781235 |
Format(781234.576,"0.0") | Rounds to 1 decimal place without separators between thousands | 781234.6 |
Format(781234.576,"0.00") | Rounds to 2 decimal place without separators between thousands | 781234.58 |
Format(781234.576,"#,##0.00") | Rounds to 2 decimal place with separators between thousands | 781,234.58 |
Format(781234.576,"$#,##0.00") | Displays dollar sign and Rounds to 2 decimal place with separators between thousands | $781,234.58 |
Format(0.576,"0%") | Converts to percentage form without decimal place | 58% |
Format(0.5768,"0%") | Converts to percentage form with two decimal places | 57.68% |
Private Sub
Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button5.Click, Button4.Click, Button3.Click
Label1.Text = Format(8972.234, "0.0")
Label2.Text = Format(8972.2345, "0.00")
Label3.Text = Format(6648972.265, "#,##0.00")
Label4.Text = Format(6648972.265, "$#,##0.00")
Label5.Text = Format(0.56324, "0%")
End Sub
The Output window is shown in the Figure 15.2 below:
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy