Lesson 20

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.

Lesson focus:

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

Lesson20
TopicFormat Function
Main FocusFormatting Numbers, Dates and Time
Key SkillBuilt-in and User-Defined Formats
Next StepCheckbox
20.1 Number Formats
20.2 User Formats
20.3 Date and Time
20.4 Real-Time Display

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.

Table 20.1: Built-in Number Formats
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%

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.

VB2015 Figure 20.1 Built-in number format output

Figure 20.1: Output of built-in number formats

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.

Table 20.2: User-Defined Number Formats
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%

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.

VB2015 Figure 20.2 User-defined number format output

Figure 20.2: Output of user-defined number formats

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.

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.

Table 20.3: Predefined Date and Time Formats
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

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.

VB2015 Figure 20.3 Predefined date and time output

Figure 20.3: Output of predefined date and time formats

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.

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.

Table 20.4: User-Defined Date and Time Formats
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

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.

VB2015 Figure 20.4 User-defined date and time output

Figure 20.4: Output of user-defined date and time formats

Why the Format Function Matters

Core takeaway:

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.

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.

Explore VB2026 β†’

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the difference between the built-in format style "Standard" and the user-defined format "#,##0.00"?
  2. Write a short VB2015 example that displays the current date in dd/mm/yyyy format.
  3. Why is the Format function useful in financial and reporting applications?

Go to Lesson 21

In the next lesson, you will learn how to work with the CheckBox control in Visual Basic 2015.