Format Function
The Format function in Visual Basic 2015 is a very useful function for displaying numbers, dates, and times in a cleaner and more user-friendly way. In real applications, raw values are often difficult to read, so formatting helps present the information in ways that users can understand immediately.
In this lesson, you will learn both the built-in formatting styles and the user-defined formatting styles for numbers, dates, and times in Visual Basic 2015.
Lesson Overview
20.1
Format Function for Numbers
The Format function can display numeric values in several common styles. These include ordinary numbers, numbers with decimal places, values with separators between thousands, currency values, and percentages.
Format(n, "style argument")
Here, n is the number to be formatted.
| Style argument | Explanation | Example |
|---|---|---|
| General Number | Displays the number without separators between thousands. | Format(8972.234, "General Number") = 8972.234 |
| Fixed | Displays the number without separators between thousands and rounds it to two decimal places. | Format(8972.234, "Fixed") = 8972.23 |
| Standard | Displays the number with separators between thousands and rounds it to two decimal places. | Format(6648972.265, "Standard") = 6,648,972.27 |
| Currency | Displays the number with a dollar sign, separators between thousands, and two decimal places. | Format(6648972.265, "Currency") = $6,648,972.27 |
| Percent | Converts the number to percentage form and appends the % sign. | Format(0.56324, "Percent") = 56.32% |
20.1 Example 20.1
Using Built-in Number Formats
Private Sub BtnFormat_Click(sender As Object, e As EventArgs) Handles BtnFormat.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
This example demonstrates how one number can appear in very different forms depending on the style argument used.
Figure 20.1: Output of built-in number formats
20.1(b)
User-Defined Number Formats
Besides the built-in styles, you can also define your own formatting patterns.
Format(n, "user's format")
User-defined formats let you control decimal places, separators, currency symbols, and percentage appearance more precisely.
| Format | Description | Output |
|---|---|---|
| Format(781234.576,"0") | Rounds to whole number without separators | 781235 |
| Format(781234.576,"0.0") | Rounds to 1 decimal place without separators | 781234.6 |
| Format(781234.576,"0.00") | Rounds to 2 decimal places without separators | 781234.58 |
| Format(781234.576,"#,##0.00") | Rounds to 2 decimal places with separators | 781,234.58 |
| Format(781234.576,"$#,##0.00") | Displays a dollar sign with separators and 2 decimal places | $781,234.58 |
| Format(0.576,"0%") | Converts to percentage without decimals | 58% |
| Format(0.5768,"0.00%") | Converts to percentage with 2 decimal places | 57.68% |
20.1 Example 20.2
Using User-Defined Number Formats
Private Sub BtnFormat_Click(sender As Object, e As EventArgs) Handles BtnFormat.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
User-defined formats are especially useful when you want the displayed value to match a business, accounting, reporting, or UI design requirement.
Figure 20.2: Output of user-defined number formats
20.2
Formatting Date and Time
The Format function is not limited to numbers. It can also display dates and time in many useful forms. This is especially important in applications such as calendars, billing systems, appointment systems, clocks, and reports.
20.2(a)
Predefined Date and Time Formats
Visual Basic 2015 provides several predefined date and time styles that can be used directly with the current system date and time via the Now function.
| Format | Description |
|---|---|
| Format(Now, "General Date") | Displays current date and time |
| Format(Now, "Long Date") | Displays current date in long format |
| Format(Now, "Short Date") | Displays current date in short format |
| Format(Now, "Long Time") | Displays current time in long format |
| Format(Now, "Short Time") | Displays current time in short format |
20.2 Example 20.3
Using Predefined Date and Time Formats
Private Sub BtnDisplay_Click(sender As Object, e As EventArgs) Handles BtnDisplay.Click
Label1.Text = Format(Now, "General Date")
Label2.Text = Format(Now, "Long Date")
Label3.Text = Format(Now, "Short Date")
Label4.Text = Format(Now, "Long Time")
Label5.Text = Format(Now, "Short Time")
End Sub
This program shows several common display styles for the current date and current time.
Figure 20.3: Output of predefined date and time formats
20.2 Real-Time Display
Displaying Date and Time in Real Time
You can display date and time in real time by using a Timer control. Set the timerβs Enabled property to True and set its interval, for example to 100.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Format(Now, "General Date")
Label2.Text = Format(Now, "Long Date")
Label3.Text = Format(Now, "Short Date")
Label4.Text = Format(Now, "Long Time")
Label5.Text = Format(Now, "Short Time")
End Sub
This makes the displayed date and time update continuously while the program runs.
20.2(b)
User-Defined Date and Time Formats
Besides predefined formats, you can also define your own formatting patterns for dates and times. The syntax is:
Format(expression, style)
This gives you more control over how the month, day, year, and time are displayed.
| Format | Description |
|---|---|
| Format(Now, "m") | Displays current month and date |
| Format(Now, "mm") | Displays current month in double digits |
| Format(Now, "mmm") | Displays abbreviated name of the current month |
| Format(Now, "mmmm") | Displays full name of the current month |
| Format(Now, "dd/mm/yyyy") | Displays current date in day/month/year format |
| Format(Now, "mmm,d,yyyy") | Displays current date in Month, Day, Year format |
| Format(Now, "h:mm:ss tt") | Displays current time in hour:minute:second format with am/pm |
| Format(Now, "MM/dd/yyyy h:mm:ss tt") | Displays current date and time together |
20.2 Example 20.4
Using User-Defined Date and Time Formats
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Format(Now, "m")
Label2.Text = Format(Now, "mm")
Label3.Text = Format(Now, "mmm")
Label4.Text = Format(Now, "mmmm")
Label5.Text = Format(Now, "dd/mm/yyyy")
Label6.Text = Format(Now, "mmm,d,yyyy")
Label7.Text = Format(Now, "h:mm:ss tt")
Label8.Text = Format(Now, "MM/dd/yyyy h:mm:ss tt")
End Sub
This example shows how flexible user-defined formatting can be for building digital clocks, calendars, dashboards, and information panels.
Figure 20.4: Output of user-defined date and time formats
20.3
Why the Format Function Matters
The Format function makes program output easier to read, more professional, and more suitable for real users. It is especially important in finance, reporting, accounting, time-based systems, dashboards, and any application that must present values in a clean and meaningful way.
Recommended Upgrade
Build on This Foundation
Continue to VB2026
After learning the basics of formatting in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.
Visual Basic Programming
Use this Top Release book to reinforce your tutorial learning with a more structured guide.
Practice
Exercise Questions
- What is the difference between the built-in format style "Standard" and the user-defined format "#,##0.00"?
- Write a short VB2015 example that displays the current date in dd/mm/yyyy format.
- Why is the Format function useful in financial and reporting applications?