The Format function is a very powerful formatting function which can display the 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 format 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 15.1.
Table 15.1 List of style arguments
|
Example 15.1
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, "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")
End SubThe Output window is shown below:
(ii) The format of the user-defined Format function isFormat (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 15.2Table15.2: User-Defined format
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 15.2
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 SubThe Output window is shown below:
http://www.vbtutor.net/vb2008/vb2008tutor.html
No comments:
Post a Comment