>
VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Code 中文VB About Us

Lesson 15 : The Format Function


The Format function in Visual Basic 2010 displays the numeric values in different forms. There are two types of Format functions in Visual Basic 2010, one of them is the built-in format function while another one is defined by the users.

15.1 The Built-in Format Function

The syntax of a built-in Format function is as follows :

Format (n, "style argument")

The list of style arguments is given in Table 15.1.

Table 15.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 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 Sub

The Output is shown in Figure 15.1.

Visual Basic 2010

Figure 15.1

15.2 The User-defined Format Function

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 follow certain formatting styles. Examples of user-defined formatting style are listed in Table 15.2

Table 15.1 List of User-defined style arguments

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%

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 Sub

The Output window is shown in Figure 15.2.

Visual Basic 2010

Figure 15.2


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy